Register a tornado atexit handler to disconnect from rabbitmq

(imported from commit b70650070f1df548794a9e3ff2948d134fd0c5de)
This commit is contained in:
Leo Franchi
2013-04-16 11:49:03 -04:00
parent 79a94a8e79
commit d7a33485ad
2 changed files with 11 additions and 0 deletions

View File

@@ -5,6 +5,7 @@ import simplejson
import random
import time
import threading
import atexit
from collections import defaultdict
# This simple queuing library doesn't expose much of the power of
@@ -46,6 +47,10 @@ class SimpleQueueClient(object):
queue=queue,
consumer_tag=self._generate_ctag(queue)))
def close(self):
if self.connection:
self.connection.close()
def ready(self):
return self.channel is not None
@@ -180,6 +185,10 @@ if settings.RUNNING_INSIDE_TORNADO and settings.USING_RABBITMQ:
elif settings.USING_RABBITMQ:
queue_client = SimpleQueueClient()
def setup_tornado_rabbitmq():
# When tornado is shut down, disconnect cleanly from rabbitmq
atexit.register(lambda: queue_client.close())
# We using a simple lock to prevent multiple RabbitMQ messages being
# sent to the SimpleQueueClient at the same time; this is a workaround
# for an issue with the pika BlockingConnection where using