2016-03-31 90 views
2

我想用Celery定期運行一些函數(任務),比如說每3秒運行一次。定期運行Celery任務(沒有Django)

最近我得到的是隻運行一次任務。

這是我的芹菜配置文件:

# celeryconfig.py 
from datetime import timedelta 

BROKER_URL = 'amqp://[email protected]//' 

CELERY_RESULT_BACKEND = 'rpc://' 

CELERYBEAT_SCHEDULE = { 
    'f1-every-3-seconds': { 
     'task': 'tasks.f1', 
     'schedule': timedelta(seconds=3), 
     'args': (1, 2) 
    }, 
    'f2-every-3-seconds': { 
     'task': 'tasks.f2', 
     'schedule': timedelta(seconds=3), 
     'args': (3, 4) 
    }, 
} 

這是我宣佈的任務:

# tasks.py: 
import celeryconfig 
from celery import Celery 
from celery import task 

dbwapp = Celery('tasks') 
dbwapp.config_from_object(celeryconfig) 

@dbwapp.task() 
def f1(a, b): 
    print "F1: {0}, {1}".format(a, b) 

@dbwapp.task() 
def f2(a, b): 
    print "F2: {0}, {1}".format(a, b) 

而這正是我的主程序可以運行:

#tasks_runner.py: 
from tasks import f1, f2, dbwapp 


f1.delay(5, 6) 
f2.delay(7, 8) 

我使用:python tasks_runner.py運行我的代碼,但沒有設法使這兩個函數定期運行。這是我得到的輸出:

[2016-03-31 23:36:16,108: WARNING/Worker-9] F1: 5, 6 
[2016-03-31 23:36:16,109: WARNING/Worker-6] F2: 7, 8 

我在做什麼錯了? 如何讓f1和f2定期運行?

回答

1

使用你的代碼,我能開始芹菜包括。計劃任務是這樣的:

$ celery beat                   (env: celery) 
celery beat v3.1.23 (Cipater) is starting. 
__ - ... __ -  _ 
Configuration -> 
    . broker -> redis://localhost:6379/0 
    . loader -> celery.loaders.default.Loader 
    . scheduler -> celery.beat.PersistentScheduler 
    . db -> celerybeat-schedule 
    . logfile -> [stderr]@%INFO 
    . maxinterval -> now (0s) 
[2016-04-01 00:15:05,377: INFO/MainProcess] beat: Starting... 
[2016-04-01 00:15:08,402: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:08,410: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:11,403: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:11,411: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:14,404: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:14,412: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:17,404: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:17,412: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:20,405: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:20,413: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:23,406: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:23,413: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 
[2016-04-01 00:15:26,407: INFO/MainProcess] Scheduler: Sending due task f2-every-3-seconds (tasks.f2) 
[2016-04-01 00:15:26,414: INFO/MainProcess] Scheduler: Sending due task f1-every-3-seconds (tasks.f1) 

它顯然加載芹菜的默認配置和啓動beat服務根據當前的配置開始 發射計劃任務。

無論如何,這隻發送請求來執行任務,但錯過實際的工人。工人可 開始在另一個控制檯:

$ celery worker -A tasks 
[2016-04-01 00:31:46,950: WARNING/MainProcess] [email protected] ready. 
[2016-04-01 00:31:47,029: WARNING/Worker-4] F2: 3, 4 
[2016-04-01 00:31:47,029: WARNING/Worker-2] F1: 1, 2 
[2016-04-01 00:31:47,036: WARNING/Worker-3] F2: 3, 4 
[2016-04-01 00:31:47,036: WARNING/Worker-1] F1: 1, 2 
[2016-04-01 00:31:48,829: WARNING/Worker-4] F2: 3, 4 
[2016-04-01 00:31:48,829: WARNING/Worker-2] F1: 1, 2 

如果你想只用一個工人,你可以一次與beat服務啓動:

$ celery worker -A tasks -B 
相關問題