时光如瑾雨微凉 2020-02-20
安装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
如图,接收到任务并成功执行。