简体   繁体   中英

Threading an Infinite Loop

def check_incoming_messages_to_client(incoming_chat_messages,uri_str, kill_threads_subscript):
    global kill_threads
    messaging = Pyro4.Proxy(uri_str)

    while(TRUE):
        if(messaging.get_connection() == 'yes'):
            msg = messaging.read_messages_to_client()
            if (msg):
                incoming_chat_messages.insert(END, msg)
        if(kill_threads[kill_threads_subscript]):
           print('break loop')
           break

print('start')
t1 = Thread(target=check_incoming_messages_to_client(incoming_chat_messages[length-1],uri_str, kill_threads_subscript))
t1.setDaemon(True)
t1.start()
print('end')

The code above only print start and not end . That means it was stuck in the infinite loop, which must not be because it was threaded. How will I fix it?

Thread(target=check_incoming_messages_to_client(incoming_chat_messages[length-1],uri_str, kill_threads_subscript)) calls your function, then passes the result as the target (except since it never ends, no result ever materializes, and you never even construct the Thread ).

You want to pass the function uncalled , and the args separately so the thread calls it when it runs, rather than the main thread running it before the worker thread even launches:

t1 = Thread(target=check_incoming_messages_to_client,
            args=(incoming_chat_messages[length-1], uri_str, kill_threads_subscript))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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