Celery unable to register tasks several cases

Celery processing program without waiting for the asynchronous task that the job end can continue with other tasks, or return the data results, is applicable in the processing time-consuming tasks such as sending mail, send a message authentication code and other scenes!

  Celery use of flexible, there is a question of deployment and use different methods, often encounter based on specific business: when using the command to start Celery, the task is not registered or other errors, resulting in not normal call Celery, for example where several common case, the description of the error and solutions, for your reference!

1. The version of the problem

Django and Flask or used in Linux and window Celery, different versions or different extensions (such as djangocelery 3.3.0 djangocelery 0.1.1 Flask-Celery 2.4.3 Flask-Celery3 celery 4.3.0 , etc.) would not lead to use Celery! I use Flask framework with celery 4.3.0 version, use normal!
----------------

2. No registration tasks

Below the code, if the task is not added to autodiscover_tasks Celery method, the effect is also no! 

"" "Boot file" ""
from Celery Celery Import
from celery_tasks Import celeryconfig
 
# define celery app: name
celery_app = Celery ( "YOYO")
 
# load configuration
celery_app.config_from_object (celeryconfig)
 
# auto-discovery and registration Tasks
celery_app.autodiscover_tasks ([ "celery_tasks.test1",
                               "celery_tasks.test2",
                               "celery_tasks.test3",])

3. Code or grammar wrong

If you use a custom Celery decorative function, elsewhere called, then tasks.py name is fixed, must be the name, it can be detected!

 

When using the decorator decorative function, task and not tsaks!

And invoking the function member 4. The process defined in the same file Celery is also afford effective!
5. Use of external references or other references in this document can not know!

Celery is because the program can be run independently, requiring separate start, if the other objects referenced in the task processing functions, is not recognized, for example as shown below:

 

from pymongo import MongoClient
 
from celery_tasks.main import celery_app
import datetime
 
# from settings import MongoClient_conn
MongoClient_conn = MongoClient('127.0.0.1', 27017)  # 连接mongodb

 

 If you are using from settings import MongoClient_conn way to introduce a database connection from the project master configuration file settings, you can not afford to be effective, because the settings py script does not know what it is to be introduced MongoDB client connections for the job! In celery can configure the connection profile, this line is quoted in celery task!

These are some of the experiences I use Celery, I hope useful for everyone!
----------------
Disclaimer: This article is CSDN blogger, "I have changed _ I have not changed." Original article, follow the CC 4.0 BY-SA copyright agreements, please attach a reprint the original source link and this statement.
Original link: https: //blog.csdn.net/anyedianxia/article/details/90763069

Guess you like

Origin www.cnblogs.com/qiu-hua/p/12143703.html