简体   繁体   English

线程无限循环

[英]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 . 上面的代码仅打印start而没有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 ). Thread(target=check_incoming_messages_to_client(incoming_chat_messages[length-1],uri_str, kill_threads_subscript)) 调用您的函数,然后将结果作为target传递(除非它永远不会结束,永远不会实现,甚至都不会构造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: 您想分别传递uncall函数和args函数,以便线程在运行时调用它,而不是在工作线程启动之前运行它的主线程:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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