The celery is a "self-powered" task queue.
Operating Environment:
- django==1.11.4
- pymysql==0.8.1
- configparser==3.5.0
- django-crontab==0.7.1
- celery==3.1.25
- redis==3.2.8
Project list:
- Add the following in the project's settings.py file:
Broker_url ='redis://127.0.0.1:6379'Celery_result_backend='redis://127.0.0.1:6379'celery_accept_content= ['Application/json']celery_task_serializer='JSON'Celery_result_serializer='JSON'Celery_timezone='Asia/shanghai
2. Add the celery.py file to the project directory, adding the following:
#Coding=utf8#Autor:vglede#TIME:2018/6/26 16:53#File:celery.py from __future__ ImportAbsolute_importImportOS fromCeleryImportCelery fromDjango.confImportSettings#Set the default Django settings module for the ' Ops ' program.Os.environ.setdefault ('Django_settings_module','ops.settings') App= Celery ('Release')#Using A string here means the worker is not having to#pickle The object when using Windows.App.config_from_object ('django.conf:settings') App.autodiscover_tasks (Lambda: Settings. Installed_apps) @app. Task (Bind=True)defDebug_task (self):Print('Request: {0!r}'. Format (self.request))
3. Under Project __init__.py Add the following code
from import app as Celery_app
4. Add a task under the registered application (release):
# Coding=utf8 # Autor:vglede # Time:2018/6/27 10:46 # File:release_api.py from celery import Task@task def Release_async_bash (src_info): print ( " job[release_async_bash" include%s is running "% (str (src_info))) result = return result
5. Using the task function name, use the delay () method to start the task.
# Somecode
exec_order= "haha"release_async_bash.delay (Exec_order)
6. Create a PID and log storage directory:
mkdir -p/var/run/celery/mkdir -p/var/log/celery/
7. Start celery (Attach the editor's script):
# coding=utf8# autor:vglede# Time:2018/6/ - -: -# file:contorl_celery.SH#!/bin/SH# Case " $" inchstart) Celery multi start W1-A Ops-lInfo--pidfile=/var/run/celery/%N.pid--logfile=/var/log/celery/%N.log;; Stop)if[ ! -F $PIDFILE] Then Echo "$PIDFILE does not exist, process was not running" ElsePidfile=`ls/var/run/celery/*. PID ' pid=$ (cat $PIDFILE) echo "stopping ..." celery multi stop w1-a ops-l Info--pidfile=/var/run/celery/%n.pid--logfile=/var/log/celery/%n.log While [-x/proc/${pid}] does echo "waiting for celery to shutdown ..." Sleep 1 done echo "celery stopped" fi; restart) if [!-F $PIDFILE] then echo ' $PIDFILE does not exist, process was not running ' else pidfile= ' ls/var/run/celery/*.pid ' pid=$ (cat $PIDFILE) echo "Stoppin G ... "Celery multi stop w1-a ops-l info--pidfile=/var/run/celery/%n.pid --logfile=/var/log/celery/%n.log while [-x/proc/${pid}] do EC Ho "Waiting for celery to shutdown ..." Sleep 1 done echo "celery stopped" fi celery multi start w1-a ops-l I NFO--pidfile=/var/run/celery/%n.pid--logfile=/var/log/celery/%n.log; *) echo "Please use Start or stop as first argument";; Esac
Learn more, refer to celery3.1.7 documentation (http://docs.jinkan.org/docs/celery/getting-started/first-steps-with-celery.html)
Python3----celery of asynchronous tasks