Answer the question
In order to leave comments, you need to log in
How to add script delay in multithreading?
There is a script which is executed in a flow. It is necessary that after the execution of all active threads - the script is turned off for the N-th number of seconds, and then calls the function to restart, i.e. create a recursion. I tried to do this after the for loop, but the time.sleep function is called while the thread is still running. How can a function be called after all active threads have terminated?
import time
from threading import Thread
class MyThread(Thread):
def __init__(self, name):
Thread.__init__(self)
self.name = name
def run(self):
msg = "%s is running" % self.name
print(msg)
def create_threads():
for i in range(5):
name = "Thread #%s" % (i + 1)
my_thread = MyThread(name)
my_thread.start()
time.sleep(5)
create_threads()
if __name__ == "__main__":
create_threads()
Answer the question
In order to leave comments, you need to log in
Wait for them to complete by calling the join method of each of the threads, or use one of the synchronization primitives for the same purpose.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question