7 周期任務(wù)

celery beat 按照間隔運(yùn)行任務(wù)捐康,被集群中可用的工作進(jìn)程來(lái)運(yùn)行。

添加周期任務(wù):

from celery import Celery
from celery.schedules import crontab

app = Celery()

@app.on_after_configure.connect
def setup_periodic_tasks(sender, **kwargs):
    # Calls test('hello') every 10 seconds.
    sender.add_periodic_task(10.0, test.s('hello'), name='add every 10')

    # Calls test('world') every 30 seconds
    sender.add_periodic_task(30.0, test.s('world'), expires=10)

    # Executes every Monday morning at 7:30 a.m.
    sender.add_periodic_task(
        crontab(hour=7, minute=30, day_of_week=1),
        test.s('Happy Mondays!'),
    )

@app.task
def test(arg):
    print(arg)

app.conf.beat_schedule = {
    'add-every-30-seconds': {
        'task': 'tasks.add',
        'schedule': 30.0,
        'args': (16, 16)
    },
}
app.conf.timezone = 'UTC'

crontab

app.conf.beat_schedule = {
    # Executes every Monday morning at 7:30 a.m.
    'add-every-monday-morning': {
        'task': 'tasks.add',
        'schedule': crontab(hour=7, minute=30, day_of_week=1),
        'args': (16, 16),
    },
}

例子:

crontab() Execute every minute.
crontab(minute=0, hour=0) Execute daily at midnight.
crontab(minute=0, hour='/3') Execute every three hours: midnight, 3am, 6am, 9am, noon, 3pm, 6pm, 9pm.
crontab(minute=0,
hour='0,3,6,9,12,15,18,21')
Same as previous.
crontab(minute='
/15') Execute every 15 minutes.
crontab(day_of_week='sunday') Execute every minute (!) at Sundays.
crontab(minute='',
hour='
', day_of_week='sun')
Same as previous.
crontab(minute='/10',
hour='3,17,22', day_of_week='thu,fri')
Execute every ten minutes, but only between 3-4 am, 5-6 pm, and 10-11 pm on Thursdays or Fridays.
crontab(minute=0, hour='
/2,/3') Execute every even hour, and every hour divisible by three. This means: at every hour except: 1am, 5am, 7am, 11am, 1pm, 5pm, 7pm, 11pm
crontab(minute=0, hour='
/5') Execute hour divisible by 5. This means that it is triggered at 3pm, not 5pm (since 3pm equals the 24-hour clock value of “15”, which is divisible by 5).
crontab(minute=0, hour='/3,8-17') Execute every hour divisible by 3, and every hour during office hours (8am-5pm).
crontab(0, 0, day_of_month='2') Execute on the second day of every month.
crontab(0, 0,
day_of_month='2-30/3')
Execute on every even numbered day.
crontab(0, 0,
day_of_month='1-7,15-21')
Execute on the first and third weeks of the month.
crontab(0, 0, day_of_month='11',
month_of_year='5')
Execute on the eleventh of May every year.
crontab(0, 0,
month_of_year='
/3')
Execute on the first month of every quarter.

solar schedules

from celery.schedules import solar

app.conf.beat_schedule = {
    # Executes at sunset in Melbourne
    'add-at-melbourne-sunset': {
        'task': 'tasks.add',
        'schedule': solar('sunset', -37.81753, 144.96715),
        'args': (16, 16),
    },
}

根據(jù)日出日落時(shí)間坎吻,指定經(jīng)緯度

dawn_astronomical Execute at the moment after which the sky is no longer completely dark. This is when the sun is 18 degrees below the horizon.
dawn_nautical Execute when there’s enough sunlight for the horizon and some objects to be distinguishable; formally, when the sun is 12 degrees below the horizon.
dawn_civil Execute when there’s enough light for objects to be distinguishable so that outdoor activities can commence; formally, when the Sun is 6 degrees below the horizon.
sunrise Execute when the upper edge of the sun appears over the eastern horizon in the morning.
solar_noon Execute when the sun is highest above the horizon on that day.
sunset Execute when the trailing edge of the sun disappears over the western horizon in the evening.
dusk_civil Execute at the end of civil twilight, when objects are still distinguishable and some stars and planets are visible. Formally, when the sun is 6 degrees below the horizon.
dusk_nautical Execute when the sun is 12 degrees below the horizon. Objects are no longer distinguishable, and the horizon is no longer visible to the naked eye.
dusk_astronomical Execute at the moment after which the sky becomes completely dark; formally, when the sun is 18 degrees below the horizon.

啟動(dòng)

celery -A proj beat

或者可以在啟動(dòng)普通工作進(jìn)程時(shí)把定時(shí)任務(wù)工作進(jìn)程嵌入

celery -A proj worker -B

因?yàn)樾枰涗浬洗芜\(yùn)行時(shí)間,以上命令需要celery在當(dāng)前路徑有讀寫(xiě)權(quán)限宇葱,也可以指定:

celery -A proj beat -s /home/celery/var/run/celerybeat-schedule

默認(rèn)使用的scheduler類是 [celery.beat.PersistentScheduler]是把上次運(yùn)行時(shí)間保存在了本地的shelve數(shù)據(jù)庫(kù)瘦真,可以把它存在django的數(shù)據(jù)庫(kù)中這樣好管理。

pip install django-celery-beat

INSTALLED_APPS = (
    ...,
    'django_celery_beat',
)

python manage.py migrate

celery -A proj beat -l info -S django
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末贝搁,一起剝皮案震驚了整個(gè)濱河市吗氏,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌雷逆,老刑警劉巖弦讽,帶你破解...
    沈念sama閱讀 218,386評(píng)論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場(chǎng)離奇詭異膀哲,居然都是意外死亡往产,警方通過(guò)查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,142評(píng)論 3 394
  • 文/潘曉璐 我一進(jìn)店門某宪,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)仿村,“玉大人,你說(shuō)我怎么就攤上這事兴喂“遥” “怎么了?”我有些...
    開(kāi)封第一講書(shū)人閱讀 164,704評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵衣迷,是天一觀的道長(zhǎng)畏鼓。 經(jīng)常有香客問(wèn)我,道長(zhǎng)壶谒,這世上最難降的妖魔是什么云矫? 我笑而不...
    開(kāi)封第一講書(shū)人閱讀 58,702評(píng)論 1 294
  • 正文 為了忘掉前任,我火速辦了婚禮汗菜,結(jié)果婚禮上让禀,老公的妹妹穿的比我還像新娘挑社。我一直安慰自己,他們只是感情好巡揍,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,716評(píng)論 6 392
  • 文/花漫 我一把揭開(kāi)白布痛阻。 她就那樣靜靜地躺著,像睡著了一般吼肥。 火紅的嫁衣襯著肌膚如雪录平。 梳的紋絲不亂的頭發(fā)上麻车,一...
    開(kāi)封第一講書(shū)人閱讀 51,573評(píng)論 1 305
  • 那天缀皱,我揣著相機(jī)與錄音,去河邊找鬼动猬。 笑死啤斗,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的赁咙。 我是一名探鬼主播钮莲,決...
    沈念sama閱讀 40,314評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開(kāi)眼,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼彼水!你這毒婦竟也來(lái)了崔拥?” 一聲冷哼從身側(cè)響起,我...
    開(kāi)封第一講書(shū)人閱讀 39,230評(píng)論 0 276
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤凤覆,失蹤者是張志新(化名)和其女友劉穎链瓦,沒(méi)想到半個(gè)月后,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體盯桦,經(jīng)...
    沈念sama閱讀 45,680評(píng)論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡慈俯,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,873評(píng)論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了拥峦。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片贴膘。...
    茶點(diǎn)故事閱讀 39,991評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖略号,靈堂內(nèi)的尸體忽然破棺而出刑峡,到底是詐尸還是另有隱情,我是刑警寧澤玄柠,帶...
    沈念sama閱讀 35,706評(píng)論 5 346
  • 正文 年R本政府宣布突梦,位于F島的核電站,受9級(jí)特大地震影響随闪,放射性物質(zhì)發(fā)生泄漏阳似。R本人自食惡果不足惜狡蝶,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,329評(píng)論 3 330
  • 文/蒙蒙 一屋确、第九天 我趴在偏房一處隱蔽的房頂上張望踱启。 院中可真熱鬧弯菊,春花似錦、人聲如沸畜吊。這莊子的主人今日做“春日...
    開(kāi)封第一講書(shū)人閱讀 31,910評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)玲献。三九已至殉疼,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間捌年,已是汗流浹背瓢娜。 一陣腳步聲響...
    開(kāi)封第一講書(shū)人閱讀 33,038評(píng)論 1 270
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒(méi)想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留礼预,地道東北人眠砾。 一個(gè)月前我還...
    沈念sama閱讀 48,158評(píng)論 3 370
  • 正文 我出身青樓托酸,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親励堡。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,941評(píng)論 2 355

推薦閱讀更多精彩內(nèi)容