我正在使用Celery在Flask应用中创建后台订阅者。当我将其保持在@ celery.task装饰器之外时,我创建的通道似乎确实起作用。但是,当我将它们安排为芹菜任务时,在芹菜控制台中会出现错误。
下面是我的频道启动:
connection = pika.BlockingConnection(pika.ConnectionParameters(
host=cfg.RABBIT_HOST
))
channel = connection.channel()
channel.exchange_declare(exchange='daemon', exchange_type = 'fanout')
result = channel.queue_declare(exclusive=True)
queue_name = result.method.queue
channel.queue_bind(exchange='daemon',
queue=queue_name)
这是我将其安排为芹菜任务的方式:
@celery.task()
def start_channel():
print("hello world!")
channel.start_consuming()
start_channel.delay()
if __name__ == "__main__":
app.run(threaded = True)
运行此代码时,该应用程序启动,但在celery控制台中出现错误:
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/pika/adapters/select_connection.py", line 982, in poll
self._get_max_wait())
OSError: [Errno 9] Bad file descriptor
我无法弄清楚这个错误。有人可以帮忙还是建议其他方法来在Flask中运行多个Rabbitmq订户?