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

Method handle_request

gunicorn/workers/sync.py:168–212  ·  view source on GitHub ↗
(self, listener, req, client, addr)

Source from the content-addressed store, hash-verified

166 util.close_graceful(client)
167
168 def handle_request(self, listener, req, client, addr):
169 environ = {}
170 resp = None
171 try:
172 self.cfg.pre_request(self, req)
173 request_start = datetime.now()
174 resp, environ = wsgi.create(req, client, addr,
175 listener.getsockname(), self.cfg)
176 # Force the connection closed until someone shows
177 # a buffering proxy that supports Keep-Alive to
178 # the backend.
179 resp.force_close()
180 self.nr += 1
181 if self.nr >= self.max_requests:
182 self.log.info("Autorestarting worker after current request.")
183 self.alive = False
184 respiter = self.wsgi(environ, resp.start_response)
185 try:
186 if isinstance(respiter, environ['wsgi.file_wrapper']):
187 resp.write_file(respiter)
188 else:
189 for item in respiter:
190 resp.write(item)
191 resp.close()
192 finally:
193 request_time = datetime.now() - request_start
194 self.log.access(resp, req, environ, request_time)
195 if hasattr(respiter, "close"):
196 respiter.close()
197 except OSError:
198 # pass to next try-except level
199 util.reraise(*sys.exc_info())
200 except Exception:
201 if resp and resp.headers_sent:
202 # If the requests have already been sent, we should close the
203 # connection to indicate the error.
204 self.log.exception("Error handling request")
205 util.close_graceful(client)
206 raise StopIteration()
207 raise
208 finally:
209 try:
210 self.cfg.post_request(self, req, environ, resp)
211 except Exception:
212 self.log.exception("Exception in post_request hook")

Calls 13

pre_requestMethod · 0.80
nowMethod · 0.80
createMethod · 0.80
write_fileMethod · 0.80
getsocknameMethod · 0.45
force_closeMethod · 0.45
infoMethod · 0.45
wsgiMethod · 0.45
writeMethod · 0.45
closeMethod · 0.45
accessMethod · 0.45
exceptionMethod · 0.45