每隔n秒等待完成的python执行函数

问题描述 投票:0回答:1

我想每60秒定期执行一次FUNCTION,但是如果之前的运行尚未完成,我不想再次执行FUNCTION。如果先前的运行在例如120秒后,我想立即执行一个新的FUNCTION调用。如果先前的运行在例如10s,然后我想等待50s,然后再执行新的FUNCTION调用。

请在下面查看我的实现。

我可以用例如subprocess.run或一些timeloop库,这样实现起来会更加简洁?

import time


def hello(x):
    # some logic here
    # execution could take any time between e.g. <10s, 120s>


def main(ii):
    while True:
        start = int(time.time())

        try:
            val = next(ii)
        except StopIteration as ex:
            return None

        else:
            hello(val)

            run_time_diff = int(time.time()) - start

            if run_time_diff < 60:
                time.sleep(60 - run_time_diff)


ii = iter(list[[...],[...],...[...]])
main(ii=ii)
python process execution schedule periodicity
1个回答
-2
投票

您好,您输入了return 0。在主函数中,在while中,在其他情况下,您像我一样放置了一个循环]

x = hello(val)
if x = 0:
# Your custom code after function will be excute
  main()
else:
# Re-excute the function if it not work
  hello(val)

但是我不确定我的x在定义时会执行hello()

© www.soinside.com 2019 - 2024. All rights reserved.