Django使用Celery加redis执行异步任务
简单使用
安装celery及redis
定义celery任务
项目下新建tasks.py
from celery import Celery # 创建一个Celery类的实例对象 app = Celery(‘celery_task.tasks‘, broker=‘redis://127.0.0.1:6379/8‘) # 定义任务函数 @app.task def send_register_active_email(message): with open("D:\\celery\\text.txt", ‘a‘) as f: f.write("To perform a task..." + message)
调用触发任务
run.py
from celery_tasks.tasks import send_register_active_email def register(): send_register_active_email.delay("test1\n") if __name__ == "__main__": register()
在项目目录下启动celery
celery -A tasks worker -l info
再运行run.py
如图,接收到任务并成功执行。
相关推荐
kaixinfelix 2020-07-27
waitzkj 2020-06-20
fgleeldq 2020-06-14
xinhao 2020-06-09
也许会有hui 2020-05-03
D先生 2020-05-09
hoooooolyhu 2020-04-23
iflreey 2020-03-05
taiyanghua 2020-02-14
kanpiaoxue 2020-01-31
牧码人 2020-01-25
shawroad 2020-01-07
bluetears 2019-12-26
loviezhang 2019-12-15
Burgesszheng 2019-12-13
liusarazhang 2019-12-06
taiyanghua 2019-12-03