简体   繁体   English

时间后运行功能

[英]Run function after t time

I got a problem is: have a variable total time is t_total after a time in t_total i want to run some function and the time continue count up or down to the end of t_total . 我有一个问题是:在t_total某个时间后有一个可变的总时间是t_total ,我想运行一些函数,并且该时间继续向上或向下计数到t_total It look like: ---t1---t2-- when time equal t1 run a function and in t2 run another function. 它看起来像: ---t1---t2--当时间等于t1时运行一个函数,而在t2中时运行另一个函数。 I tried to make a count function: 我试图做一个计数功能:

for t in range(t_total):
    t = t + 1
    time.sleep(1)
    if t = t1:
        function1()

but if follow that function the count up will stop at time when run function1. 但是如果遵循该功能,则在运行function1时,计时将停止。 So, have anyway to keep the count continue to run the function2? 那么,是否仍要保持计数继续运行function2?

You can do using thread 你可以使用线程

import threading
for t in range(t_total):
    t = t + 1
    time.sleep(1)
    if t = t1:
        threading.Thread(target=function1).start()

Add into function 1 to start a new timer starting at time t. 添加到功能1中以在时间t开始启动一个新计时器。 You can export the value of t, t1 and t2 into function 1 and continue a new timer. 您可以将t,t1和t2的值导出到函数1中并继续使用新的计时器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM