Often using Python to develop Web applications, will involve the timing of the task of the script, formerly with the Linux crontab to operate, but feel not too grounded gas, and later found that with Celery+django can be easily implemented!
The installation software environment is as follows:
Python 2.7.5
django==1.8.2
celery==3.1.18
celery-with-redis==3.0
django-celery==3.1.16
mysql-python==1.2.3
supervisor==3.1.3
Use Pip to install the above software, and the default system already has Redis and MySQL server installed!
First create Project:
django-admin.py CreateProject Picha
Then create an app named Demo:
django-admin.py Startapp Demo
The directory structure of the project is:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M00/7A/03/wKiom1agmSST4mpHAAFz4NX9Bg4001.jpg "title=" Tree.jpg "alt=" Wkiom1agmsst4mphaafz4nx9bg4001.jpg "/>
Second, configure the celery-related configuration in the settings file:
# celery Stuffimport djcelerydjcelery.setup_loader () Broker_url = ' redis://localhost:6379 ' CELERYBEAT_SCHEDULER = ' Djcelery.schedulers.DatabaseScheduler ' # timed task celery_result_backend = ' Djcelery.backends.database:DatabaseBackend ' Celery_result_backend = ' redis://localhost:6379 ' celery_accept_content = [' Application/json ']CELERY_TASK_SERIALIZER = ' json ' Celery_result_serializer = ' json ' celery_timezone = ' Asia/shanghai '
Installed_apps = (' Django.contrib.admin ', ' Django.contrib.auth ', ' django.contrib.contenttypes ', ' django.contr Ib.sessions ', ' django.contrib.messages ', ' Django.contrib.staticfiles ', ' demo ', ' Djcelery ',)
Then modify the city:
Time_zone = ' Asia/shanghai '
The city is wrong, the scheduled task will not be executed by time!
In addition, we also need to create a celery.py file, he will automatically find the task! under our app
#! /usr/bin/env python# coding:utf-8from __future__ import absolute_importimport osfrom celery import Celeryfrom Django.con F Import settings# Set the default Django settings module for the ' Celery ' program.os.environ.setdefault (' Django_settings_ MODULE ', ' picha.settings ') app = celery (' Picha ') # Using A string here means the worker won't have 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) def debug_task (self): print (' Request: {0!r} '. Format (self.request))
Now we create a test task! under the demo app
From __future__ import absolute_importfrom celery import shared_task,task@shared_task () def add (x, y): # return x + y Print X + y@shared_task () def mul (x, y): print "%d *%d =%d"% (x,y,x*y) return X*y@shared_task () def sub (x, y): Prin T "%d-%d =%d"% (x,y,x-y) return X-y@task (ignore_result=true,max_retries=1,default_retry_delay=10) def just_print (): Print "Print from celery task"
Here, the Django and celery sections have been installed!
Three I now start configuring supervisor to start the relevant celery program:
1) Initialize the Supervisor configuration file!
echo_supervisord_conf >/etc/supervisord.conf
2) then add the following configuration at the end of the supervisord.conf file:
[program:djangoproject.celeryd]command=/usr/local/pyenv/shims/python /usr/local/coding/pythoner/picha/ Manage.py celeryd --concurrency=1user=rootnumprocs=1directory=/usr/local/coding/pythoner/pichastdout _logfile=/var/log/celery_worker.logstderr_logfile=/var/log/celery_worker.logautostart=trueautorestart= truestartsecs=10stopwaitsecs = 120priority=998[program:djangoproject.celerybeat]command=/usr/local/ pyenv/shims/python /usr/local/coding/pythoner/picha/manage.py celery beat --schedule=/tmp/ Celerybeat-schedule --pidfile=/tmp/django_celerybeat.pid --loglevel=infouser=rootnumprocs=1directory =/usr/local/coding/pythoner/pichastdout_logfile=/var/log/celery_beat.logstderr_logfile=/var/log/celery_ Beat.logautostart=trueautorestart=truestartsecs=10stopwaitsecs = 120priority=998[program: djangoproject.celerycam]command=/usr/local/pyenv/shims/python /usr/local/coding/pythoner/picha/manage.py celerycam --frequency=10.0user=rootnumprocs=1directory=/usr/local/coding/pythoner/pichastdout_logfile=/var/log/celerycam.logstderr_logfile=/ var/log/celerycam.logautostart=trueautorestart=truestartsecs=10stopwaitsecs = 120priority=998
Four now we need to synchronize the celery related library files to MySQL, we use the command:
Python manage.py syncdb
Then create the Superuser
Django-admin manage.py Createsuperuser
Start Supervisor:
Supervisord-d
to see if the service started successfully, use the command supervisorctl status
Djangoproject.celerybeat RUNNING pid 3061, uptime 1:03:27
Djangoproject.celerycam RUNNING pid 3063, uptime 1:03:27
Djangoproject.celeryd RUNNING pid 3062, uptime 1:03:27
Then we went into the django-admin backstage,
Now we start Django:
Python manage.py runserver 0.0.0.0:8008
After entering the background, click "Periodic Tasks":
You can see the method written in the tasks.py below, in the drop-down menu appears, we only choose the corresponding time!
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/04/wKiom1ago13D_WhAAACwT_ocy4s066.png "title=" Qq20160121172117.png "alt=" Wkiom1ago13d_whaaacwt_ocy4s066.png "/>
Now, let's start choosing the time to schedule the task:
We create a timed task that does not 10s,print a value and is placed in the log file for viewing:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/02/wKioL1agpDvgqiviAABBus0fwxA090.png "title=" 326. PNG "alt=" Wkiol1agpdvgqiviaabbus0fwxa090.png "/>
We view log files:
650) this.width=650; "src=" Http://s3.51cto.com/wyfs02/M02/7A/02/wKioL1agpH_gHaO1AABQZc6BUb0321.png "title=" Task-1.png "alt=" wkiol1agph_ghao1aabqzc6bub0321.png "/> Match our settings in the background of the web!
We're setting up an addition operation every 15s, and we can dynamically modify the parameters on the backend of the Web platform,
For the first time, we pass in Parameters 9 and 5, and the result should be 14, and we'll look at the settings and logs:
650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/7A/02/wKioL1agpPSSMJ3wAABhrOzHmk0255.png "title=" 72411. PNG "alt=" Wkiol1agppssmj3waabhrozhmk0255.png "/>
I'll look at the log again:
650) this.width=650; "src=" Http://s1.51cto.com/wyfs02/M00/7A/02/wKioL1agpSPBSEG9AABE04bbRJk013.png "title=" Task-2.png "alt=" Wkiol1agpspbseg9aabe04bbrjk013.png "/>
Then we modify the incoming parameters in the Web background to 10 and 7, do not restart the service, the calculated result changes dynamically to 17!
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7A/04/wKiom1agpVbyw9hYAAA69d0kMYg405.png "title=" Diff.png "alt=" Wkiom1agpvbyw9hyaaa69d0kmyg405.png "/>
We found that the result data has changed dynamically!
If we launch the Supervisor script:/usr/local/coding/pythoner/picha/manage.py Celerycam--frequency=10.0
You can view Woker in admin background is not online:
650) this.width=650; "src=" Http://s2.51cto.com/wyfs02/M02/7A/03/wKioL1agpirRGgfvAABHkqS5MS4227.png "title=" Worker.png "alt=" wkiol1agpirrggfvaabhkqs5ms4227.png "/>celery-django related configuration is complete!
PS: The results of scheduled tasks in the configuration process can only be viewed in the log, do not know how to display in the background of admin, if you know, can tell me, 3q!
This article is from the "Shine_forever blog" blog, make sure to keep this source http://shineforever.blog.51cto.com/1429204/1737323
Use Celery+django to set up scheduled tasks in admin background