[英]Challenges getting simple Huey example started with Crontab
我正在嘗試將 Huey 文檔中建議的代碼組織實施到現有應用程序中,並遵循簡單的示例。 目標是構建一個每天凌晨 3:00 運行任務的 crontab。
我打開了兩個終端選項卡,第一個是消費者運行示例中的腳本:
PYTHONPATH=.:$PYTHONPATH
export WORKER_CLASS=${1:-thread}
huey_consumer.py main.huey --workers=4 -k $WORKER_CLASS -C -S
然后,在另一個選項卡中,我運行 main.py 腳本:
python main.py
from huey import SqliteHuey
huey = SqliteHuey(filename='/tmp/huey.db')
from config import huey
# Note that this time is 1 minute before whenever I'm debugging.
# I'm using the 3 am example as what we're aiming for in our final product.
@huey.periodic_task(crontab(minute='0', hour='3'))
def run_this_function():
system = New_Class() # Class instantiation since there's a bunch that happens when a new class is set up
system.run_method # Runs a bunch of methods from the class in one location
from config import huey
from tasks import run_this_function
def main:
run_this_function()
if __name__ == "__main__":
main()
該任務立即運行,並且由於我對 Huey 是全新的,不確定我可能會缺少什么以使其按計划工作。 我嘗試了很多 crontab 組合,不確定是否存在挑戰,或者我如何在main
方法中調用run_this_function
。 任何幫助表示贊賞!
因此,首先,您實際上並不想自己調用run_this_function()
,因此無需在另一個選項卡中運行main.py
腳本。 您只需要讓 huey 消費者運行,因為您希望它負責在請求的時間執行任務。
您的消費者需要能夠發現任務,您可以通過將其導入到該主文件中來執行此操作,然后通過該文件(您也在執行此操作)啟動 huey 實例。 一個簡單的測試可能是將打印語句放在與您定義周期性任務的文件相同的文件中。 運行消費者后,您應該會看到您的打印語句。 如果沒有,您的任務也不會被消費者接走。
其次,我建議不要使用crontab
function。 我過去無法讓它工作,相反,你最好編寫自己的 function 來配置凌晨 3 點。 Huey 使用當前時間戳定期調用提供的 function。 因此,作為一個可重用的示例,您可以執行以下操作:
def run_at_my_preferred_hour(hour_to_run):
last_day_it_ran_for = None
def check_if_task_needs_to_run_for_this_time(current_timestamp):
if current_timestamp.hour == hour_to_run and current_timestamp.day != last_day_it_ran_for:
# Save that you already ran the task for this day
last_day_it_ran_for = current_timestamp.day
return True
return False
return check_if_task_needs_to_run_for_this_time
@huey.periodic_task(run_at_my_preferred_hour(3))
def run_this_function():
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.