最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

python - Questions about the role of routing_key in using Redis as a celery message broker - Stack Overflow

programmeradmin1浏览0评论

[celery_config.py]

task_queues = ( 
    Queue( 'test_queue', 
    exchange=Exchange('test_queue', type='topic'), 
    routing_key='test_queue.*' ), 
)

task_routes = { 
    'game.tasks.add': 
        { 'queue': 'test_queue', 
          'exchange' : 'test_queue', 
          'exchange_type' : 'topic', 
          'routing_key' : 'test_queue.add', 
          'serializer' : 'json' }, 
    'game.tasks.mul': { 
        'queue': 'test_queue', 
        'exchange' : 'test_queue', 
        'exchange_type' : 'topic', 
        'routing_key' : 'onetwothree', 
        'serializer' : 'json' }, 
}  

I think the mul task should not come into test_queue due to celery settings. But this comes in. (Due to routing_key) I want to know the cause.

If type is direct, it works as I want. Is it a problem with topic exchange_type?

[Actuall Happend] When exchange type is set to direct

$ python manage.py shell

>>> from game.tasks import *
>>> result = add.delay(4, 5)
>>> result = mul.delay(5, 4)

$ celery -A mysite woker -l info
[queues]
> test_queue       exchange=test_queue(direct) key=test_queue.add
                

[tasks]
  . game.tasks.add
  . game.tasks.mul

[2025-03-13 09:10:48,335: INFO/MainProcess] Connected to redis://************/0

[2025-03-13 09:10:48,338: INFO/MainProcess] mingle: searching for neighbors

[2025-03-13 09:10:49,347: INFO/MainProcess] mingle: all alone

[2025-03-13 09:10:51,641: INFO/MainProcess] Task 
game.tasks.add[086f143f-246b-45f7-8512-f8160beaefdc] received

[Actually Happened] When exchange type is set to topic

$ python manage.py shell

from game.tasks import *
>>> result = add.delay(4, 5)
>>> result = mul.delay(5, 4)

$ celery -A mysite woker -l info
[queues]
                
.> test_queue       exchange=test_queue(topic) key=test_queue.*
                

[tasks]
  . game.tasks.add
  . game.tasks.mul

[2025-03-13 09:19:26,328: INFO/MainProcess] Connected to redis://************/0

[2025-03-13 09:19:26,330: INFO/MainProcess] mingle: searching for neighbors

[2025-03-13 09:19:27,338: INFO/MainProcess] mingle: all alone

[2025-03-13 09:20:55,541: INFO/MainProcess] Task game.tasks.add[86ab6351-ff2f-4585-88b2-1fa95c6c50e9] received

[2025-03-13 09:20:57,096: INFO/MainProcess] Task game.tasks.mul[6bc76458-64e3-45e1-8a3d-7266574219ec] received

[The situation I expected]

When I specify exchange_type as topic, I don't think the multask should come in because routing_key is not right, so why is it coming in?

发布评论

评论列表(0)

  1. 暂无评论