当前位置: 首页 > 工具软件 > Celery > 使用案例 >

Celery、flower和celery-redis 的安装与使用

邵捷
2023-12-01

0x01 安装celery

pip install Celery

0x02 安装celery-redis

celery-redis:如果把redis当做broker,需要安装

pip install -U celery-with-redis
pip install MySQL-python

0x03 安装flower

pip install flower

0x04 启动flower

/usr/local/bin/python2.7 /usr/local/bin/flower --port=7788 --broker=redis://localhost:6379/0 --broker_api=redis://localhost:6379/0

0x05 使用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



tips:http://blog.csdn.net/jazywoo123/article/details/17636615

 类似资料: