我对Celery中的信号处理有疑问。我需要能够触发一些关闭逻辑,以便与我的Cassandra集群/会话(使用DataStax Python驱动程序)正常断开连接。我尝试了两种不同的方法来实现SIGTERM和SIGINT处理(参见下面的代码),为信号分配拆卸功能。我使用的是Ubuntu 16.04 LTS。
celerytest.py
import logging
import signal
from celery import Celery
from celery import platforms
from celery.signals import beat_init
from celery.signals import worker_process_init
from cassandra.cluster import Cluster
cluster = None
session = None
log = logging.getLogger()
log.setLevel('INFO')
handler = logging.StreamHandler()
handler.setFormatter(logging.Formatter("%(asctime)s [%(levelname)s] %(name)s: %(message)s"))
log.addHandler(handler)
def cassandra_init(**kwargs):
global cluster, session
""" Initialize a clean Cassandra connection. """
if cluster is not None:
log.info("Shutting down Cassandra cluster connection")
cluster.shutdown()
if session is not None:
log.info("Shutting down Cassandra session connection")
session.shutdown()
log.info("Initializing Cassandra connection")
cluster = Cluster(['127.0.0.1'])
session = cluster.connect('test')
def cassandra_teardown(signum, frame):
log.info("Shutting down Cassandra session connection")
session.shutdown()
log.info("Shutting down Cassandra cluster connection")
cluster.shutdown()
def install_pool_process_sighandler(**kwargs):
log.info("Installing sighandlers")
platforms.signals["TERM"] = cassandra_teardown # DOES NOT WORK
platforms.signals["INT"] = cassandra_teardown # DOES NOT WORK
#signal.signal(signal.SIGINT, cassandra_teardown) # DOES NOT WORK EITHER
#signal.signal(signal.SIGTERM, cassandra_teardown) # DOES NOT WORK EITHER
# Initialize worker context for both standard and periodic tasks.
worker_process_init.connect(cassandra_init)
worker_process_init.connect(install_pool_process_sighandler)
beat_init.connect(cassandra_init)
beat_init.connect(install_pool_process_sighandler)
app = Celery('celerytest', broker_url = 'amqp://guest:guest@localhost:5672//')
按ctrl-c应该触发 cassandra_teardown 功能,但不会。我在这里缺少什么?
输出
^C
worker: Hitting Ctrl+C again will terminate all running tasks!
worker: Warm shutdown (MainProcess)