celery 文档上的用法好像是声明好 exchange,queue,绑定好之后往队列里发,但是我现在的场景是直接发到 exchange 里就可以,queue 谁来绑定我并不关心,我也不要去做声明用 kombu 自己实现很容易,但是 celery 有什么方法做到吗,我看到的都是直接往 queue 里发.消息队列用的 rmq,类型是 topic,顺便求 flask+celery+rmq 的的最佳实践.以下是我用 kombu 的实现
def send_as_task( exchange_name, args=(), kwargs={}, routing_key=''):
exchange = Exchange(name=exchange_name, type='topic', durable=True, auto_delete=False)
payload = {'args': args, 'kwargs': kwargs}
with producers[config_use.get_connection()].acquire(block=True) as producer:
producer.publish(body=payload,
serializer='json',
compression='bzip2',
exchange=exchange,
declare=[exchange],
routing_key=routing_key,
retry=True,
retry_policy={
'interval_start': 0, # First retry immediately,
'interval_step': 2, # then increase by 2s for every retry.
'interval_max': 30, # but don't exceed 30s between retries.)
'max_retries': 30, # give up after 30 tries.
},
)