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

可不可以把 Celery 的应用写在不同的文件里?

  •  
  •   tdifg · Jul 12, 2015 · 3830 views
    This topic created in 4147 days ago, the information mentioned may be changed or developed.

    搜了一阵,发现现在网上的例子里都是在一个文件里写完所有可能要执行的任务,上面注册上celery的task。
    但是,我想写一个插件式的程序,它会动态搜出插件(在一个目录下,不同的文件里)里的指定函数并执行。插件结构是:
    proj
    --plugins
    ----xx.py
    ----yy.py
    --mycelery.py
    --controller.py
    所有插件都会使用mycelery.py里的app来注册task。在控制器里动态导入插件,然后执行指定函数。
    可以这样写吗?或者哪里有什么使用案例,看了一天英文,还没怎么看懂……

    5 replies  •  2015-07-14 09:19:38 +08:00
    neoblackcap
    neoblackcap
    Jul 13, 2015
    1
    当然是可以的,只要你去配置好celery就可以了,让它启动的时候能去对应的目录载入函数。
    tdifg
    tdifg
    OP
    Jul 13, 2015
    2
    @neoblackcap 就是那个include参数吗?尝试过一次,但总是报各种import找不到路径的错。主要是搞不清楚这里路径相对关系。
    tdifg
    tdifg
    OP
    Jul 13, 2015
    3
    发现这个网址
    http://stackoverflow.com/questions/20905462/python-django-celery-project-structure
    这里说的Is it possible that Django project uses the non-django celery project for all celery tasks.
    情况很相近
    neoblackcap
    neoblackcap
    Jul 13, 2015
    4
    @tdifg 对,然后该路径使用绝对路径就好了
    tdifg
    tdifg
    OP
    Jul 14, 2015
    5
    @neoblackcap 的确,但是task修饰的函数里import其他目录库的路径和你启动celery worker的路径还是相关的(相对路径),所以调好了位置后就不敢动了。
    这句话好绕……
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   930 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 20:36 · PVG 04:36 · LAX 12:36 · JFK 15:36
    ♥ Do have faith in what you're doing.