将任务延迟到某一时刻 [英] delay a task until certain time

查看:135
本文介绍了将任务延迟到某一时刻的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在python脚本中做的是睡几秒钟,直到达到所需的时间。 IE:如果runAt设置是15:20,当前时间是10:20,我该如何计算出几秒钟睡眠?我不知道如何将15:20转换成一个时间和当前日期,然后扣除实际的时间来获得秒数。

What I want to do in a python script is sleep a number of seconds until the required time is reached. IE: if runAt setting is 15:20 and current time is 10:20, how can I work out how many seconds to sleep? I'm not sure how to convert 15:20 to a time and current date then deduct the actual time to get the seconds.

推荐答案

p>想想您也可以使用以下代码:

Think you can also use the following code:

from datetime import datetime, time
from time import sleep

def act(x):
    return x+10

def wait_start(runTime, action):
    startTime = time(*(map(int, runTime.split(':'))))
    while startTime > datetime.today().time(): # you can add here any additional variable to break loop if necessary
        sleep(1)# you can change 1 sec interval to any other
return action

wait_start('15:20', lambda: act(100))

这篇关于将任务延迟到某一时刻的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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