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
yongzhong
V2EX  ›  Python

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

  •  
  •   yongzhong · 2015-12-23 09:59:12 +08:00 · 2862 次点击
    这是一个创建于 3048 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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 条回复    2015-12-24 09:51:33 +08:00
    yongzhong
        1
    yongzhong  
    OP
       2015-12-23 10:34:20 +08:00
    找到问题所在了...请求 move 或删除 @Livid
    latyas
        2
    latyas  
       2015-12-23 13:01:05 +08:00
    类方法好像不是这么用吧?
    latyas
        3
    latyas  
       2015-12-23 13:01:24 +08:00
    漏打字了。。
    类方法作为 task 好像不是这么用吧?
    tempdban
        4
    tempdban  
       2015-12-23 14:51:05 +08:00 via Android
    顶楼上 直接继承 task 类
    yongzhong
        5
    yongzhong  
    OP
       2015-12-23 15:46:00 +08:00
    tempdban
        6
    tempdban  
       2015-12-24 09:51:33 +08:00 via Android
    @yongzhong ok,thanks
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   857 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:44 · PVG 04:44 · LAX 13:44 · JFK 16:44
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.