Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从Crontab开始获取简单Huey示例的挑战_Python_Python 3.x_Python Huey - Fatal编程技术网

Python 从Crontab开始获取简单Huey示例的挑战

Python 从Crontab开始获取简单Huey示例的挑战,python,python-3.x,python-huey,Python,Python 3.x,Python Huey,我正在尝试将Huey docs中的应用程序实现到现有的应用程序中,并遵循。目标是构建一个crontab,它将在每天凌晨3:00运行任务 我启动了两个终端选项卡,第一个是消费者运行示例中的脚本: PYTHONPATH=.:$PYTHONPATH export WORKER_CLASS=${1:-thread} huey_consumer.py main.huey --workers=4 -k $WORKER_CLASS -C -S 然后,在另一个选项卡中,我运行main.py脚本: python

我正在尝试将Huey docs中的应用程序实现到现有的应用程序中,并遵循。目标是构建一个crontab,它将在每天凌晨3:00运行任务

我启动了两个终端选项卡,第一个是消费者运行示例中的脚本:

PYTHONPATH=.:$PYTHONPATH
export WORKER_CLASS=${1:-thread}
huey_consumer.py main.huey --workers=4 -k $WORKER_CLASS -C -S
然后,在另一个选项卡中,我运行main.py脚本:

python main.py
config.py tasks.py main.py
这项任务马上就要开始了,因为我对休伊来说是个新手,所以我不确定我可能会错过什么来让它按计划工作。我尝试了很多crontab组合,不确定是否存在挑战,也不确定如何调用
main
方法中的
run\u this\u函数。感谢您的帮助

首先,您实际上不想自己调用
run\u this\u函数()
,因此不需要在另一个选项卡中运行
main.py
脚本。您只需要让huey consumer运行,因为您希望它负责在请求的时间执行任务

您的使用者需要能够发现任务,您可以在执行此操作时将其导入主文件,然后通过该文件启动huey实例(您也在执行此操作)。一个简单的测试可以是将print语句放在定义定期任务的同一个文件中。运行consumer后,您应该会看到打印语句。如果没有,您的任务也不会被消费者接受

其次,我建议不要使用
crontab
函数。我过去一直无法让它工作,相反,您最好编写自己的函数来配置3AM。Huey使用当前时间戳定期调用提供的函数。因此,作为一个可重复使用的示例,您可以执行以下操作:

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():
   ...

这可以说是太宽泛了。有大量有关
cron
作业的疑难解答提示。(我注意到您未能导出
PYTHONPATH
,但可能还有其他需要检查的内容。)只需运行
main.py
即可调用
run\u this\u函数()
,方法是使用最后的标准
\u name\uuuu
逻辑。感谢您提供的Cronjob信息。我在这里专门寻找Huey Python库实现。使用Huey任务包装器,它应该调用函数,但将该作业设置为稍后运行,如果可以,我希望将所有内容都保留在库中。感谢您的澄清。我对休伊不熟悉,我想我没有完全理解你的问题,但我想我现在对你想问的问题有了更好的了解。您确定
任务
作业到时也不会运行它吗?你可能根本不想
main
调用函数,只想
import tasks
或者对其中的代码进行评估;如果您在凌晨3点之前开始运行
huey\u consumer.py main.huey--workers=4-k thread-C-S
,然后运行
tasks.py
,它是否会在凌晨3点触发该功能?
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()
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():
   ...