Celery factory function versus importing celery

I am developing a Flask application and am using drawings. I plan on using celery task queues. I'm trying to understand the benefit or reason to use something like

def make_celery(app): celery = Celery(app.import_name, broker=app.config['CELERY_BROKER_URL']) celery.conf.update(app.config) TaskBase = celery.Task class ContextTask(TaskBase): abstract = True def __call__(self, *args, **kwargs): with app.app_context(): return TaskBase.__call__(self, *args, **kwargs) celery.Task = ContextTask return celery 

and then do

 celery = make_celery(app) 

and import it into my .py tasks compared to importing and instantiating celery in my tasks. Such as

 from celery import Celery app = Celery('hello', broker='amqp:// guest@localhost //') @app.task def mytask(): 
+5
source share
1 answer

If you are writing a simple task, it is better to import celery and decorate your function.

If you are creating several complex tasks, it is best to assign a task. Here you get the power of OOP. You can break your code into small blocks. This makes unit test your code easier. In addition, if you want to create your own configuration for all your tasks, you can create your own base class, and you can inherit it for all tasks.

+2
source

All Articles