Celery+python+redis asynchronous execution of timed tasks

Source: Internet
Author: User
Tags install redis

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.