mirror of
https://github.com/zulip/zulip.git
synced 2025-10-26 01:24:02 +00:00
tornado: Send request_started signal in Django thread.
Django’s ASGIHandler does this too and it seems like a good idea.
Signed-off-by: Anders Kaseorg <anders@zulip.com>
(cherry picked from commit a7e10ee47e)
This commit is contained in:
committed by
Tim Abbott
parent
6b3399d7e6
commit
a303c27a16
@@ -98,7 +98,7 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
|||||||
descriptor = get_descriptor_by_handler_id(self.handler_id)
|
descriptor = get_descriptor_by_handler_id(self.handler_id)
|
||||||
return f"AsyncDjangoHandler<{self.handler_id}, {descriptor}>"
|
return f"AsyncDjangoHandler<{self.handler_id}, {descriptor}>"
|
||||||
|
|
||||||
def convert_tornado_request_to_django_request(self) -> HttpRequest:
|
async def convert_tornado_request_to_django_request(self) -> HttpRequest:
|
||||||
# This takes the WSGI environment that Tornado received (which
|
# This takes the WSGI environment that Tornado received (which
|
||||||
# fully describes the HTTP request that was sent to Tornado)
|
# fully describes the HTTP request that was sent to Tornado)
|
||||||
# and pass it to Django's WSGIRequest to generate a Django
|
# and pass it to Django's WSGIRequest to generate a Django
|
||||||
@@ -111,7 +111,9 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
|||||||
# Django's WSGIHandler.__call__ before the call to
|
# Django's WSGIHandler.__call__ before the call to
|
||||||
# `get_response()`.
|
# `get_response()`.
|
||||||
set_script_prefix(get_script_name(environ))
|
set_script_prefix(get_script_name(environ))
|
||||||
signals.request_started.send(sender=self.__class__)
|
await sync_to_async(
|
||||||
|
lambda: signals.request_started.send(sender=self.__class__), thread_sensitive=True
|
||||||
|
)()
|
||||||
request = WSGIRequest(environ)
|
request = WSGIRequest(environ)
|
||||||
|
|
||||||
# We do the import during runtime to avoid cyclic dependency
|
# We do the import during runtime to avoid cyclic dependency
|
||||||
@@ -149,7 +151,7 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
|||||||
self.finish()
|
self.finish()
|
||||||
|
|
||||||
async def get(self, *args: Any, **kwargs: Any) -> None:
|
async def get(self, *args: Any, **kwargs: Any) -> None:
|
||||||
request = self.convert_tornado_request_to_django_request()
|
request = await self.convert_tornado_request_to_django_request()
|
||||||
response = await sync_to_async(lambda: self.get_response(request), thread_sensitive=True)()
|
response = await sync_to_async(lambda: self.get_response(request), thread_sensitive=True)()
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -228,7 +230,7 @@ class AsyncDjangoHandler(tornado.web.RequestHandler, base.BaseHandler):
|
|||||||
# to automatically return our data in its response, and call
|
# to automatically return our data in its response, and call
|
||||||
# Django's main self.get_response() handler to generate an
|
# Django's main self.get_response() handler to generate an
|
||||||
# HttpResponse with all Django middleware run.
|
# HttpResponse with all Django middleware run.
|
||||||
request = self.convert_tornado_request_to_django_request()
|
request = await self.convert_tornado_request_to_django_request()
|
||||||
|
|
||||||
# We import RequestNotes during runtime to avoid
|
# We import RequestNotes during runtime to avoid
|
||||||
# cyclic import
|
# cyclic import
|
||||||
|
|||||||
Reference in New Issue
Block a user