django celery redis

# celery redis

celery 执行异步 定时 任务等的工作

架构:

1.  broker  : 前端接收消息,

2. worker  : 消费消息

3. backend  :  消费消息的结果写入

实验:

redis + celery

redis

安装: apt install redis-server

启动:  redis-server,  配置在 /etc/redis/redis.conf: 未详细了解

连接:  redis-cli -h ... -p ...

使用: 可以 keys * 查看当前 redis中保存了哪些键值

celery的使用:

新建 task.py 文件:

app 指定了 前端消息队列,和后端保存的redis的配置

@task 装饰器, 定义了两个任务  add  sendmail

from celery import task, Celery

from lmutils import debug_info

app = Celery('tasks', broker='redis://localhost:6379/0',

backend='redis://localhost:6379/0')

@task

def add(x, y):

return x + y

@task

def sendmail(mail):

print(debug_info(), "+"*10)

print(debug_info(), "sending mail to {}".format(mail))

time.sleep(2.0)

print(debug_info(), "send done.")

print(debug_info(),  "+"*10)

return mail['to']

启动celery服务:

celery -A task worker --loglevel=info

在task.py文件同目录下, 编写客户端:

from task  import add, sendmail

import redis,  json

conn = redis.Connection()

task = add.delay(1,2)

conn.send_command("get celery-task-meta-{}".format(task.task_id))

res = conn.read_response()

res =

你可能感兴趣的:(django celery redis)