In one of my previous articles, I wrote "Celery+django+redis Asynchronous execution Task"
Blog: http://blog.csdn.net/apple9005/article/details/54236212
You will find that the code does not rely on the Django framework and can be easily executed in a py file, because the code does not use the Django-celery,django-redis and other things that are attached to the Django framework.
Today, with an example of an official document, test the asynchronous execution of a celery task. I executed it once within the Django framework, no problem; now write a py file directly, and try it out at the command line.
1. Environment construction
Installing celery, Redis via PIP
Pip Install celery
Pip Install Redis
Default installation celery The latest version is 4.0.2
The default installation of Redis latest version is 2.10.5
Also do not forget to install the Redis service:
# wget http://download.redis.io/releases/redis-3.2.6.tar.gz# tar xzf redis-3.2.6.tar.gz# cd redis-3.2.6# make# src/redis-server # 启动redis服务,看清楚,这是在你的redis-3.2.6目录下执行的# src/redis-cli # 启动客户端
2. Write the PY
Direct Vim got up
$ vim tesks.py
From celeryImport celeryFrom Celery.schedulesImport Crontabapp = Celery (' Tasks ', broker=' redis://localhost:6379/0 ')@app. On_after_configure.connectDefSetup_periodic_tasks(sender, **kwargs): # Calls test (' hello ') every seconds. Sender.add_periodic_task ( 10.0, Test.s ( ' hello '), Name= ' Add Every ') # Calls Test (' world ') every seconds Sender.add_periodic_task (30.0, Test.s ( ' world '), Expires=10) # executes every Monday morning at the a.m. Sender.add_periodic_task (crontab (Hour=7, Minute=30, Day_of_week=1), Test.s ( @app. Taskdef test
Hold down the SHIFT key +z+z, save the file and exit
3. Start the Redis service in the new command window
How do I start?
Go to your Redis directory
$ cd redis-3.2.6$ src/redis-3.2.6
4. Start celery in a new window
Enter the directory where you tasks.py, execute:
$ celery -A tasks worker --loglevel=info
At this point, the asynchronous service is available, but the scheduled task is not executed, what's the matter? You also need to start another service, as follows
5. Start beat in a new window
Or to enter your tasks.py directory, execute:
$ celery -A tasks beat
What it looks like after it's started:
Look at the 4th step of the window, is there a printout? good! Print Hello every 10 seconds, print once every 30 seconds world!
For details on how long your project will be executed, see the official documentation for the parameter description.
Attached Official document address: http://docs.celeryproject.org/en/latest/
Attached: How to monitor celery?
Web monitoring Management Service-flower of parallel processing framework celery
See links
https://my.oschina.net/u/2306127/blog/420929
Http://flower-docs-cn.readthedocs.io/zh/latest/index.html
Celery+python+redis asynchronous execution of timed tasks