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

Method handle_request

gunicorn/workers/base_async.py:199–257  ·  view source on GitHub ↗
(self, listener_name, req, sock, addr)

Source from the content-addressed store, hash-verified

197 self.log.exception("Exception in post_request hook")
198
199 def handle_request(self, listener_name, req, sock, addr):
200 request_start = datetime.now()
201 environ = {}
202 resp = None
203 try:
204 self.cfg.pre_request(self, req)
205 resp, environ = wsgi.create(req, sock, addr,
206 listener_name, self.cfg)
207 environ["wsgi.multithread"] = True
208 self.nr += 1
209 if self.nr >= self.max_requests:
210 if self.alive:
211 self.log.info("Autorestarting worker after current request.")
212 self.alive = False
213
214 if not self.alive or not self.cfg.keepalive:
215 resp.force_close()
216
217 respiter = self.wsgi(environ, resp.start_response)
218 if self.is_already_handled(respiter):
219 return False
220 try:
221 if isinstance(respiter, environ['wsgi.file_wrapper']):
222 resp.write_file(respiter)
223 else:
224 for item in respiter:
225 resp.write(item)
226 resp.close()
227 finally:
228 request_time = datetime.now() - request_start
229 self.log.access(resp, req, environ, request_time)
230 if hasattr(respiter, "close"):
231 respiter.close()
232 if resp.should_close():
233 raise StopIteration()
234 except StopIteration:
235 raise
236 except OSError:
237 # If the original exception was a socket.error we delegate
238 # handling it to the caller (where handle() might ignore it)
239 util.reraise(*sys.exc_info())
240 except Exception:
241 if resp and resp.headers_sent:
242 # If the requests have already been sent, we should close the
243 # connection to indicate the error.
244 self.log.exception("Error handling request")
245 try:
246 sock.shutdown(socket.SHUT_RDWR)
247 sock.close()
248 except OSError:
249 pass
250 raise StopIteration()
251 raise
252 finally:
253 try:
254 self.cfg.post_request(self, req, environ, resp)
255 except Exception:
256 self.log.exception("Exception in post_request hook")

Callers 1

handleMethod · 0.95

Calls 15

is_already_handledMethod · 0.95
nowMethod · 0.80
pre_requestMethod · 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

Tested by

no test coverage detected