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