安装celery
pip install Celery
celery 手册: https://www.celerycn.io/ru-men/celery-chu-ci-shi-yong
安装celery-redis
celery-redis:如果把redis当做broker,需要安装
pip install -U celery-with-redis
pip install MySQL-python
安装flower
pip install flower
启动flower
/usr/local/bin/python2.7 /usr/local/bin/flower --port=7788 --broker=redis://localhost:6379/0 --broker_api=redis://localhost:6379/0
使用celery
[root@MyServer ~]# cat tasks.py
import time
from celery import Celery, platforms
platforms.C_FORCE_ROOT = True
celery = Celery('tasks', broker='redis://localhost:6379/0')
@celery.task
def add(x,y):
return x+y
接着启动celery:
celery -A tasks worker --loglevel=info
然后新建个shell,在python下,输入:
>>> from tasks import *
>>> add.delay(1,2)
<AsyncResult: 1a0a9262-7858-4192-9981-b7bf0ea7483b>
可以在worker里看到:
[2013-08-27 19:20:23,363: WARNING/MainProcess] celery@MichaeliMac.local ready.
[2013-08-27 19:20:23,367: INFO/MainProcess] consumer: Connected to redis://localhost:6379/0.
[2013-08-27 19:20:45,618: INFO/MainProcess] Got task from broker: tasks.sendmail[1a0a9262-7858-4192-9981-b7bf0ea7483b]
[2013-08-27 19:20:45,655: WARNING/PoolWorker-4] sending mail to celery@python.org...
[2013-08-27 19:20:47,657: WARNING/PoolWorker-4] mail sent.
[2013-08-27 19:20:47,658: INFO/MainProcess] Task tasks.sendmail[1a0a9262-7858-4192-9981-b7bf0ea7483b] succeeded in 2.00266814232s: 3
原文链接:https://blog.csdn.net/zzddada/article/details/104399951