I have a function that takes a long time to finish. How do I execute another function every X seconds while the long function is executed?
The script is using Python 3.4
I have a function that takes a long time to finish. How do I execute another function every X seconds while the long function is executed?
The script is using Python 3.4
Here is a quite simple example. It can still be tuned if important to you to kill the second function when the first function is finished.
import threading, time
def func_long(ev):
# do stuff here
for _ in range(12):
print("Long func still working")
time.sleep(1)
# if complete
ev.set()
def run_func(ev, nsec):
if ev.is_set():
return
print ("Here run periodical stuff")
threading.Timer(nsec, run_func, [ev, nsec]).start()
def main():
ev = threading.Event()
threading.Timer(5.0, run_func, [ev, 5]).start()
func_long(ev)
if __name__=='__main__':
main()