当我将“直接”类型更改为“扇出”或“主题”时,这是可行的。但是,“直接”类型却无效。
from __future__ import absolute_import, unicode_literals
from kombu import Queue, Exchange
exchange = Exchange('demo_exchange', type='topic')
demo_queues = [
Queue('one', exchange, routing_key='o'),
Queue('two', exchange, routing_key='t'),
Queue('three', exchange, routing_key='th'),
]
from __future__ import absolute_import, unicode_literals
from kombu.mixins import ConsumerMixin
from kombu import Connection
from queues import demo_queues
class Worker(ConsumerMixin):
def __init__(self, connection):
self.connection = connection
def get_consumers(self, Consumer, channel):
return [Consumer(queues=demo_queues, accept=['pickle', 'json'], callbacks=[self.on_task])]
def on_task(self, body, message):
args = body['args']
func = body['func']
print(args, func)
func()
message.ack()
if __name__ == '__main__':
with Connection('redis://localhost:6379/0') as conn:
try:
worker = Worker(conn)
worker.run()
except KeyboardInterrupt:
print('bye')
from __future__ import absolute_import, unicode_literals
from kombu.pools import producers
from queues import exchange
priority_to_routing_key = {
'high': 'o',
'mid': 't',
'low': 'th',
}
def send_tasks(conn, func, args, priority='high'):
data = {'func': func, 'args': args}
with producers[conn].acquire(block=True) as producer:
routing_key = priority_to_routing_key[priority]
producer.publish(data,
serializer='pickle',
exchange=exchange,
declare=[exchange],
routing_key=routing_key)
if __name__ == '__main__':
from kombu import Connection
from tasks import func_task
connection = Connection('redis://localhost:6379/0')
send_tasks(connection, func=func_task, args=('test hello'), priority='mid')
def func_task(n='hello'):
print(n, '---====')
我希望解决这个问题。