How to structure celery tasks

房东的猫 提交于 2019-12-03 02:26:29
NikosK

Based on celery documentation you can import a structure of celery tasks like this:

For example if you have an (imagined) directory tree like this:

|
|-- foo
|    |-- __init__.py
|    |-- tasks.py
|
|-- bar
     |-- __init__.py
     |-- tasks.py

Then calling app.autodiscover_tasks(['foo', bar']) will result in the modules foo.tasks and bar.tasks being imported.

Celery tasks can be async, sync or scheduled depends on its invocation

task.delay(arg1,arg2)       #will be async
task.delay(arg1,arg2).get() #will be sync
task.delay(arg1,arg2).get() #will be sync
task.apply_async(args = [arg1,arg2], {'countdown' : some_seconds}) #async with delay

There's a lot of invocations depending on your needs
However, you must start celery with -B flag to enable celery scheduler

$ celery worker --app=tasks -B -Q my_queue,default_queue

So the way you take to organize your tasks is something personal and it deppends on your project complexity, but I think that organize them by its type of synchronism wouldn't be the best option.

I've googled this topic and I haven't found any guide or advise, but I've read some cases that organize their task by their functionality.
I've followed this advise, because this isn't a pattern, in my projects. Here one example of how I did

your_app
    |
    -- reports
        |
        -- __init__.py
        -- foo_report.py
        -- bar_report.py
        -- tasks
            |
            -- __init__.py
            -- report_task.py
    -- maintenance
        |
        -- __init__.py
        -- tasks
            |
            -- __init__.py
            -- delete_old_stuff_task.py
    -- twitter
        |
        -- __init__.py
        -- tasks
            |
            -- __init__.py
            -- batch_timeline.py                
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!