我可以设置带有时钟时间的线程计时器以与python中的cron作业同步吗 [英] Can i set a threading timer with clock time to sync with cron job in python

查看:130
本文介绍了我可以设置带有时钟时间的线程计时器以与python中的cron作业同步吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一份cron作业,运行时间为12、12:30,1、1:30.因此,每半小时间隔一次.每当cron作业运行时,我都想在python代码中运行一个线程.

I have a cron job that runs at 12, 12:30,1, 1:30. So every half hour intervals on the clock. I want to run a thread in my python code whenever the cron job runs.

我看过每x秒/分钟运行一个计时器的示例,但是如果我在1:15 pm启动我的python代码,例如如果我每30分钟设置一个计时器,下一次它将运行在1 :下午45点我想让线程在下午1:30运行.有可能吗?

I have seen examples where to run a timer every x seconds/mintues.But if I start my python code at 1:15pm for example if I set a timer for every 30 mins, the next time it will run is at 1:45pm. I want to thread to run at 1:30pm though. Is that possible?

推荐答案

几乎一切皆有可能,真正的问题是为什么要这样做?请记住,cron调度程序不一定会在准确的时间执行作业,此外,检查Python中的时间不一定与cron的时间相对应,因此,如果您需要在之后之后在Python代码中执行某些操作>执行cron作业,您不能依赖于测量Python中的计时-实际上,您需要从cron作业通信"到Python脚本(最简单的方法是使用数据报套接字)

Almost everything is possible, the real question is why do you want to do this? Keep in mind that cron scheduler won't necessarily execute jobs at precise times and furthermore, checking the time in Python won't necessarily correspond to cron's time, so if you need to execute something in your Python code after a cron job is executed, you cannot rely on measuring timings in Python - you actually need to 'communicate' from your cron job to your Python script (the easiest would be using datagram sockets)

但是,如果要在Python中模拟cron调度程序,一种方法是使用datetime模块获取当前时间,然后计算在再次触发命令之前所需的time.sleep()数量,例如:

But, if you want to simulate cron scheduler in Python, one way to do it is to use the datetime module to get the current time and then calculate the amount of time.sleep() you need before your command fires again, e.g.:

def schedule_hourly_task(task, minute=0):
    # get hourly offset in seconds in range 1-3600
    period = ((60 + minute) % 60) * 60 or 3600
    while True:
        # get current time
        current = datetime.datetime.now()
        # count number of seconds in the current hour
        offset = (current - current.replace(minute=0, second=0, microsecond=0)).total_seconds()
        # calculate number of seconds til next period
        sleep = period - offset % period
        if offset + sleep > 3600:  # ensure sleep times apply to full hours only
            sleep += 3600 % period
        # sleep until the next period
        time.sleep(sleep)
        # run the task, break the loop and exit the scheduler if `False` is returned
        if not task():
            return

然后,您可以使用它以任何full_hour + minute偏移量运行所需的任何任务.例如:

Then you can use use it to run whatever task you want at any full_hour + minute offset. For example:

counter = 0
def your_task():
    global counter
    counter += 1
    if counter > 2:
        return False
    return True

schedule_hourly_task(your_task, 15)

,它将每隔15分钟执行一次your_task()函数,直到your_task()返回False-在这种情况下,在退出之前,在hh:15hh:30hh:45处.就您而言,只要your_task()返回True,调用schedule_hourly_task(your_task, 30)就会每30分钟调用一次your_task()函数.

And it will execute your_task() function every 15 minutes until your_task() returns False - in this case at hh:15, hh:30 and hh:45 before exiting. In your case, calling schedule_hourly_task(your_task, 30) will call the your_task() function every full 30 minutes for as long as your_task() returns True.

这篇关于我可以设置带有时钟时间的线程计时器以与python中的cron作业同步吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆