python3 celery使用

首先我们要安装一下celery

下面直接写代码列子
先创一个 proj 文件夹
proj/celery.py

from celery import Celery

# 创建celery实例
app = Celery('demo')
app.config_from_object('proj.celeryconfig')

# 自动搜索任务
app.autodiscover_tasks(['proj'])

我们把配置放到这个单独配置文件里面
proj/celeryconfig.py

BROKER_URL = 'redis://@127.0.0.1:6379/1'
CELERY_RESULT_BACKEND = 'redis://@127.0.0.1:6379/2'

proj/tasks

from proj.celery import app as celery_app

@celery_app.task
def my_task1(a,b):

    print("任务函数(my_task1)正在执行....")
    return a+b

@celery_app.task
def my_task2(c,d):
    print("任务函数(my_task2)正在执行....")
    return c+d

@celery_app.task
def my_task3(e,f):
    print("任务函数(my_task3)正在执行....")
    return e+f


我们要到终端启动一下,自动搜索任务

celery -A proj worker -l info

tasktest.py

from proj.tasks import *
my_task1.delay(1,2)
my_task2.delay(3,4)
my_task3.delay(5,6)

最后我们执行这个文件就可以看到有没有触发

报错
ValueError: not enough values to unpack (expected 3, got 0)
我们需要安装一下 eventlet

py -3.6 -m celery -A proj.celery_demo worker -l info -P eventlet
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值