Files
zulip/zerver/tornado/application.py
Anders Kaseorg c263bfdb41 queue: Use a thread-local Pika connection.
According to the documentation: “Pika does not have any notion of
threading in the code. If you want to use Pika with threading, make
sure you have a Pika connection per thread, created in that thread. It
is not safe to share one Pika connection across threads, with one
exception: you may call the connection method add_callback_threadsafe
from another thread to schedule a callback within an active pika
connection.”

https://pika.readthedocs.io/en/stable/faq.html

This also means that synchronous Django code running in Tornado will
use its own synchronous SimpleQueueClient rather than sharing the
asynchronous TornadoQueueClient, which is unfortunate but necessary as
they’re about to be on different threads.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
2022-05-02 17:41:49 -07:00

32 lines
923 B
Python

import atexit
import tornado.web
from django.conf import settings
from tornado import autoreload
from zerver.lib.queue import TornadoQueueClient
from zerver.tornado.handlers import AsyncDjangoHandler
def setup_tornado_rabbitmq(queue_client: TornadoQueueClient) -> None: # nocoverage
# When tornado is shut down, disconnect cleanly from RabbitMQ
atexit.register(lambda: queue_client.close())
autoreload.add_reload_hook(lambda: queue_client.close())
def create_tornado_application() -> tornado.web.Application:
urls = (
r"/notify_tornado",
r"/json/events",
r"/api/v1/events",
r"/api/v1/events/internal",
)
return tornado.web.Application(
[(url, AsyncDjangoHandler) for url in urls],
debug=settings.DEBUG,
autoreload=False,
# Disable Tornado's own request logging, since we have our own
log_function=lambda x: None,
)