V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
aoscici2000
V2EX  ›  Python

celery 任务调用问题

  •  
  •   aoscici2000 · 2019-12-08 15:54:11 +08:00 · 2806 次点击
    这是一个创建于 1597 天前的主题,其中的信息可能已经有所发展或是发生改变。

    config.py

    broker_url = 'amqp://guest:guest@localhost:5672/my_vhost'
    
    task_routes = {
        'project.task.create_order': {
            'queue': 'succeed'
        },
    }
    
    imports = [
        'project.task',
    ]
    

    task.py

    @app.task
    def hello(msg):
        sleep(2)
        print('hello')
    
    @app.task
    def create_order(data):
        sleep(2)
        print('create_order')
    

    我的设想是 create_order 会自动处理 succeed 里面的消息, 而 hello 是需要的时候手动调用

    以下面的命令启动后:

    celery -A project.main worker -l info -Q succeed -P eventlet -E

    create_order 正常工作, 但调用 hello 没反应

    celery -A project.main worker -l info -P eventlet -E

    去掉 -Q succeed 的话就相反了, hello 可以正常调用, 但 create_order 就不工作...

    我记得我以前试过是可以同时工作的?

    3 条回复    2019-12-08 21:07:31 +08:00
    ifoolish
        1
    ifoolish  
       2019-12-08 18:55:19 +08:00
    问题很明显了。你的配置文件中 create_order 指定了队列 succeed,你运行 worker 的参数 -Q succeed 也指定了这是 处理 succeed 队列的 worker,那当然只会执行 create_order 啊,想要 hello 也会得到执行,你再多开一个处理默认队列的 worker 进程就行了,也就是不加 -Q 参数
    ifoolish
        2
    ifoolish  
       2019-12-08 18:59:34 +08:00
    或者你只想开一个 worker 进程的话,-Q 参数加上默认队列的名字,即-Q succeed,celery。仔细看看文档吧
    aoscici2000
        3
    aoscici2000  
    OP
       2019-12-08 21:07:31 +08:00
    @ifoolish 谢谢, 确实如此, celery 貌似是自动生成了队列, 没留意到
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5479 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 06:48 · PVG 14:48 · LAX 23:48 · JFK 02:48
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.