1、新建tasks.py文件,在该文件内定义我们的功能函数,比如add

from celery import Celery

app = Celery('tasks', backend='rpc://', broker='redis://localhost')

@app.task
def add(x, y):
return x + y

2、启动redis作为消息中间件,使用内置的rpc作为结果存储地

3、启动celery

/usr/local/python365/bin/celery -A tasks worker --loglevel=info

4、在tasks.py所在目录下,新建其他文件,比如test.py,在test.py中引入tasks.py中add函数

5、运行test.py,即可实现add的调用

 

常用使用方法

result.get()        # 获取任务的返回值

result = add.delay(4,4)        # 调用任务函数

result.ready()        # 查看任务执行进度或结果 False 未完 True 结束

result.id        # 任务ID

相关文章:

  • 2021-10-15
  • 2022-12-23
  • 2021-07-19
  • 2021-07-29
  • 2021-09-16
  • 2021-10-13
  • 2022-12-23
  • 2021-10-17
猜你喜欢
  • 2021-12-12
  • 2021-10-02
  • 2022-12-23
  • 2022-12-23
  • 2022-02-16
  • 2022-01-02
相关资源
相似解决方案