推荐学习书目
› 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
yongzhong
V2EX  ›  Python

celery 多 worker 时在 run 中出现 no module named 错误

  •  
  •   yongzhong · Dec 23, 2015 · 3463 views
    This topic created in 3955 days ago, the information mentioned may be changed or developed.

    hello.py

    # coding=utf-8
    import time
    from manage import app
    from celery.contrib.methods import task_method
    
    
    class HelloService(object):
        @app.task(filter=task_method)
        def say(self):
            time.sleep(4)
            print('hello!')
    

    world.py

    # coding=utf-8
    import time
    from celery.contrib.methods import task_method
    from manage import app
    
    
    class WorldService(object):
        @app.task(filter=task_method)
        def say(self):
            time.sleep(4)
            print('hello')
    

    run.py

    # coding=utf-8
    from hello import HelloService
    from world import WorldService
    
    if __name__ == '__main__':
        HelloService().say.delay()
        WorldService().say.delay()
        print('process done')
    

    使用

    celery worker -A hello --loglevel=INFO -n hello
    celery worker -A world --loglevel=INFO -n world
    

    分别启动两个 woker

    执行 run.py
    结果在 hello 的 worker 里报错 DecodeError: No module named world
    而再 world 的 worker 里报错 DecodeError: No module named hello

    尝试在 run.py 中只保留一个 service 的调用,注释另一个的 import.则不会出现此错误

    6 replies  •  2015-12-24 09:51:33 +08:00
    yongzhong
        1
    yongzhong  
    OP
       Dec 23, 2015
    找到问题所在了...请求 move 或删除 @Livid
    latyas
        2
    latyas  
       Dec 23, 2015
    类方法好像不是这么用吧?
    latyas
        3
    latyas  
       Dec 23, 2015
    漏打字了。。
    类方法作为 task 好像不是这么用吧?
    tempdban
        4
    tempdban  
       Dec 23, 2015 via Android
    顶楼上 直接继承 task 类
    tempdban
        6
    tempdban  
       Dec 24, 2015 via Android
    @yongzhong ok,thanks
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   3181 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 30ms · UTC 14:24 · PVG 22:24 · LAX 07:24 · JFK 10:24
    ♥ Do have faith in what you're doing.