Blocking an operator in a thread leads to a longer time to kill a thread?

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)

    # thread_stop_event is of type threading.Event
    while (not thread_stop_event.is_set()):

        print "important information"
        time.sleep(5) # print some information every five seconds


    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()

    # start working threads
    for i in range(0, max_threads):
        t = Thread(target=worker_thread, args=(i,))
        running_threads.append(t)
        running_threads[i].start()

    # start thread that provides user with statistical information
    inf_thread = Thread(target=print_statistics, args=(max_threads,))
    inf_thread.start()

    # wait for user interrupt
    while True:
        input = raw_input("\nType \"quit\" to quit!\n")
        if input == "quit":
            thread_stop_event.set() # inform all threads to terminate
            break
        else:
            print "Input not recognized. Try again!"
+4
1

, ,

while not thread_stop_event.wait(5):
    print "important information"
0

All Articles