I am running a custom thread in Python that should regularly print some statistics about the current progress. As soon as the user wants to stop the program, all threads must be killed. This still works, but for the specified topic.
def print_statistics(thread_id):
print "Thread {} started (information thread)".format(thread_id)
while (not thread_stop_event.is_set()):
print "important information"
time.sleep(5)
print "Thread {} is terminating - Bye".format(thread_id)
I use threading.Event, which is disconnected as soon as the user indicates exit from the program. Then the daemon process terminates and the thread stops automatically.
I am aware of time.sleep (). Since it only blocks five seconds, the thread must be killed in the worst case after this time.
: , , , print_statistics. ? ?
, . , .
def run():
running_threads = list()
for i in range(0, max_threads):
t = Thread(target=worker_thread, args=(i,))
running_threads.append(t)
running_threads[i].start()
inf_thread = Thread(target=print_statistics, args=(max_threads,))
inf_thread.start()
while True:
input = raw_input("\nType \"quit\" to quit!\n")
if input == "quit":
thread_stop_event.set()
break
else:
print "Input not recognized. Try again!"