【问题标题】:scheduling a print command at specific timings after the script started in Python在 Python 中启动脚本后在特定时间安排打印命令
【发布时间】:2016-05-17 11:29:14
【问题描述】:

我想在不同的时间安排一个文本“hello world”(形成一个列表),因为脚本已经开始了。最好的方法是什么?

这可能是错误的,但我到目前为止:

import time
times = [1.76493425, 3.10174059, 4.49576803, 10.99379224, 18.84178369] #at these times since onset of script, a text "hello world" should be printed

start = time.time()

def main():
    for cuetime in times:
        print ('hello world', ' - timing: ', cuetime, ' - now: ', time.time()- start)
    yield viztask.waitTime(cuetime)

main()

这给了我:

('hello world', ' - timing: ', 1.76493425, ' - now: ', 0.0)
('hello world', ' - timing: ', 3.10174059, ' - now: ', 1.7699999809265137)
('hello world', ' - timing: ', 4.49576803, ' - now: ', 3.5379998683929443)
('hello world', ' - timing: ', 10.99379224, ' - now: ', 5.305999994277954)
('hello world', ' - timing: ', 18.84178369, ' - now: ', 7.075000047683716)

但我真正需要的是与“现在”时间相同的时间元素/项目,因为时间列表中的元素是相对于开始应该打印文本“hello world”的时间脚本。

【问题讨论】:

    标签: python timing benchmarking


    【解决方案1】:

    查看sched 库以获取更多详细信息。这是一个不是 100% 准确的代码示例,但如果您不需要毫秒精度,应该可以解决问题。

    import time
    import sched
    
    def print_time(cuetime):
        global start
        print ('hello world', ' - timing: ', cuetime, ' - now: ', time.time()- start)
    
    start = time.time()
    times = [1.76493425, 3.10174059, 4.49576803, 10.99379224, 18.84178369] 
    
    if __name__ == "__main__":
        s = sched.scheduler(time.time, time.sleep)
        for cuetime in times:
            s.enter(cuetime, 1, print_time, (cuetime,))
        s.run()
    

    【讨论】:

    • 感谢 Luc,调度程序绝对是要走的路
    【解决方案2】:

    schedule 模块呢?以前没有使用过它,但您可以很容易地实现您的目标:

    import schedule
    import time
    from functools import partial
    # your specified list of times
    times = [1.76493425, 3.10174059, 4.49576803, 10.99379224, 18.84178369]
    # define a job you want to do
    def job(t, start):
        print ('hello world', ' - timing: ', t, ' - now: ', time.time()- start)
        # pop the job right away from schedule.jobs, so it runs only once
        return schedule.CancelJob
    # get the starting time
    start = time.time()
    # for each time add what to do
    for t in times:
        # using partial so i can pass arguments to job
        schedule.every(t).seconds.do(partial(job, t, start))
    # and run it inside a lop
    while True:
        schedule.run_pending()
        # schedule.jobs is just a list of jobs
        if not schedule.jobs:
            break
    

    打印出来:

    hello world  - timing:  1.76493425  - now:  1.7650279998779297
    hello world  - timing:  3.10174059  - now:  3.101846933364868
    hello world  - timing:  4.49576803  - now:  4.495898962020874
    hello world  - timing:  10.99379224  - now:  10.993950605392456
    hello world  - timing:  18.84178369  - now:  18.84195566177368
    

    【讨论】:

    • 感谢 quapka,这太棒了,尤其是 cmets,使它变得透明。
    • 我很高兴它有帮助。有改进的空间。可能每次都通过start。出于某种原因,我想到了装饰器和上下文管理器。但我猜如果工作更复杂,那会很有趣。
    猜你喜欢
    • 2013-11-27
    • 1970-01-01
    • 2016-08-27
    • 2015-03-26
    • 1970-01-01
    • 2020-08-14
    • 2019-07-17
    • 2013-03-23
    • 1970-01-01
    相关资源
    最近更新 更多