Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to structure celery tasks

Tags:

python

celery

I have 2 types of task: async tasks and schedule tasks. So, here is my dir structure:

proj   |   -- tasks       |       -- __init__.py       |       -- celeryapp.py     => celery instance defined in this file.       |       -- celeryconfig.py       |       -- async       |    |       |    -- __init__.py       |    |       |    -- task1.py    => from proj.tasks.celeryapp import celery       |    |       |    -- task2.py    => from proj.tasks.celeryapp import celery       |       -- schedule            |            -- __init__.py            |            -- task1.py    => from proj.tasks.celeryapp import celery            |            -- task2.py    => from proj.tasks.celeryapp import celery 

But when I run celery worker like below, it does not work. It can not accept the task from celery beat scheduler.

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

So, is there any best practice on multiple task files organization?

like image 935
pat.inside Avatar asked Jul 17 '13 14:07

pat.inside


People also ask

How does Celery execute tasks?

Introduction. Celery is a task queue/job queue based on asynchronous message passing. It can be used as a background task processor for your application in which you dump your tasks to execute in the background or at any given moment. It can be configured to execute your tasks synchronously or asynchronously.

How does celery task queue work?

Celery communicates via messages, usually using a broker to mediate between clients and workers. To initiate a task, the Celery client adds a message to the queue, and the broker then delivers that message to a worker. The most commonly used brokers are Redis and RabbitMQ.


1 Answers

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.

like image 70
NikosK Avatar answered Sep 20 '22 08:09

NikosK