Celery定时任务之简单实现

Project Layout:
在这里插入图片描述

celerybeat-schedule文件是自动生成的

//celeryconfig.py
from datetime import timedelta

CELERYBEAT_SCHEDULE = {
	//这个add也可以是别的名字,就是给任务起个名字
    'add': {
        'task': 'celerytasks.add',
        'schedule': timedelta(seconds=3),
        'args': (16, 16)
    }
}
//celerytasks.py
from celery import Celery

 
//第一个tasks是给Celery对象起了个名字
app = Celery('tasks',broker='amqp://myuser:mypassword@localhost/myvhost',backend='amqp')

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

启动:
celery -A celerytasks worker -l info --beat

效果:

You must only enable the serializers that you will actually use.


  warnings.warn(CDeprecationWarning(W_PICKLE_DEPRECATED))
 
 -------------- celery@localhost.localdomain v3.1.18 (Cipater)
---- **** ----- 
--- * ***  * -- Linux-3.10.0-862.el7.x86_64-x86_64-with-centos-7.5.1804-Core
-- * - **** --- 
- ** ---------- [config]
- ** ---------- .> app:         tasks:0x7f6a91391a90
- ** ---------- .> transport:   amqp://myuser:**@localhost:5672/myvhost
- ** ---------- .> results:     amqp
- *** --- * --- .> concurrency: 2 (prefork)
-- ******* ---- 
--- ***** ----- [queues]
 -------------- .> celery           exchange=celery(direct) key=celery
                

[tasks]
  . tasks.add

[2018-09-28 16:48:05,163: INFO/MainProcess] Connected to amqp://myuser:**@127.0.0.1:5672/myvhost
[2018-09-28 16:48:05,179: INFO/Beat] beat: Starting...
[2018-09-28 16:48:05,230: INFO/Beat] Scheduler: Sending due task add (tasks.add)
[2018-09-28 16:48:05,236: INFO/MainProcess] mingle: searching for neighbors
[2018-09-28 16:48:06,258: INFO/MainProcess] mingle: all alone
[2018-09-28 16:48:06,305: WARNING/MainProcess] celery@localhost.localdomain ready.
[2018-09-28 16:48:07,239: INFO/MainProcess] Received task: tasks.add[26d0a4b8-471f-4f57-b5bd-b45289c1e03a]
[2018-09-28 16:48:07,278: INFO/MainProcess] Task tasks.add[26d0a4b8-471f-4f57-b5bd-b45289c1e03a] succeeded in 0.0364102920012s: 32
[2018-09-28 16:48:08,239: INFO/Beat] Scheduler: Sending due task add (tasks.add)
[2018-09-28 16:48:08,246: INFO/MainProcess] Received task: tasks.add[85fda813-7b74-4e7a-95f0-ec0775291550]
[2018-09-28 16:48:08,287: INFO/MainProcess] Task tasks.add[85fda813-7b74-4e7a-95f0-ec0775291550] succeeded in 0.0389857619994s: 32
[2018-09-28 16:48:11,240: INFO/Beat] Scheduler: Sending due task add (tasks.add)
[2018-09-28 16:48:11,243: INFO/MainProcess] Received task: tasks.add[43b3c1f1-4f57-4300-97df-993cf9d8775e]
[2018-09-28 16:48:11,275: INFO/MainProcess] Task tasks.add[43b3c1f1-4f57-4300-97df-993cf9d8775e] succeeded in 0.0129267649991s: 32

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值