MCPcopy
hub / github.com/benoitc/gunicorn / handle_request

Method handle_request

gunicorn/workers/gthread.py:657–713  ·  view source on GitHub ↗
(self, req, conn)

Source from the content-addressed store, hash-verified

655 self.log.exception("Exception in post_request hook")
656
657 def handle_request(self, req, conn):
658 environ = {}
659 resp = None
660 try:
661 self.cfg.pre_request(self, req)
662 request_start = datetime.now()
663 resp, environ = wsgi.create(req, conn.sock, conn.client,
664 conn.server, self.cfg)
665 environ["wsgi.multithread"] = True
666 self.nr += 1
667 if self.nr >= self.max_requests:
668 if self.alive:
669 self.log.info("Autorestarting worker after current request.")
670 self.alive = False
671 resp.force_close()
672
673 if not self.alive or not self.cfg.keepalive:
674 resp.force_close()
675 elif len(self.keepalived_conns) >= self.max_keepalived:
676 resp.force_close()
677
678 respiter = self.wsgi(environ, resp.start_response)
679 try:
680 if isinstance(respiter, environ['wsgi.file_wrapper']):
681 resp.write_file(respiter)
682 else:
683 for item in respiter:
684 resp.write(item)
685
686 resp.close()
687 finally:
688 request_time = datetime.now() - request_start
689 self.log.access(resp, req, environ, request_time)
690 if hasattr(respiter, "close"):
691 respiter.close()
692
693 if resp.should_close():
694 self.log.debug("Closing connection.")
695 return False
696 except OSError:
697 # pass to next try-except level
698 util.reraise(*sys.exc_info())
699 except Exception:
700 if resp and resp.headers_sent:
701 # If the requests have already been sent, we should close the
702 # connection to indicate the error.
703 self.log.exception("Error handling request")
704 util.close_graceful(conn.sock)
705 raise StopIteration()
706 raise
707 finally:
708 try:
709 self.cfg.post_request(self, req, environ, resp)
710 except Exception:
711 self.log.exception("Exception in post_request hook")
712
713 return True

Callers 1

handleMethod · 0.95

Calls 14

pre_requestMethod · 0.80
nowMethod · 0.80
createMethod · 0.80
write_fileMethod · 0.80
infoMethod · 0.45
force_closeMethod · 0.45
wsgiMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45
accessMethod · 0.45
should_closeMethod · 0.45
debugMethod · 0.45

Tested by

no test coverage detected