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

Celery 不同类型的任务配置 config,是不是要在初始化的时候,分别实例化?

  •  
  •   miniyao · Oct 5, 2017 · 3124 views
    This topic created in 3163 days ago, the information mentioned may be changed or developed.
    比如,三个异步任务,分别是:

    1、一个普通的异步任务
    2、一个每隔 10 分钟执行一次的任务
    3、每天 12 点执行一次的任务

    这三个任务都需要应用上下文支持,是不是要在初始化的时候,分别实例化?

    celery1 = Celery(__name__, ...)
    celery2 = Celery(__name__, ...)
    celery3 = Celery(__name__, ...)

    感觉好别扭啊
    5 replies    2017-10-13 17:56:34 +08:00
    Hstar
        1
    Hstar  
       Oct 5, 2017
    是在一个 Celery 里注册这三个任务....

    难道你开十个普通异步任务要十个实例吗...
    miniyao
        2
    miniyao  
    OP
       Oct 5, 2017
    @Hstar 开十个普通异步任务我知道呀,一般用装饰器装饰一下就可用 @celery.task_A() @celery.task_B() @celery.task_C() ... 这些普通异步任务都是用相同的 Celery config。

    我说的问题是定时任务,如“一个每隔 10 分钟执行一次的任务”,“每天 12 点执行一次的任务”这些配置不同啊,直接复用 @celery.task_*(),这怎么区分是普通异步任务?还是定时异步任务呢?
    junzki
        4
    junzki  
       Oct 6, 2017   ❤️ 1
    @miniyao Celery 不区分这个。Celery 的定时任务需要一个专门的守护进程 celery beep。大致原理就是 celery beep 发起一个异步任务,由 celery worker 执行。
    kevindu
        5
    kevindu  
       Oct 13, 2017
    可以在配置里面写,也可以在任务中指定

    CELERYBEAT_SCHEDULE = {
    'task_a': {
    'task': 'first_task',
    'schedule': crontab(hour=18, minute=00),
    'kwargs': {'stype': 'example'}
    },
    "task_b": {
    "task": "second_task",
    "schedule": crontab(hour=16, minute=30)
    }
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   3236 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 61ms · UTC 14:05 · PVG 22:05 · LAX 07:05 · JFK 10:05
    ♥ Do have faith in what you're doing.