2016-01-20 54 views
1

我正在学习Python中的多线程,并使用以下链接中的代码。Python中的多线程(pythonwin挂起)

http://www.tutorialspoint.com/python/python_multithreading.htm

的PythonWin的IDE挂起(无响应)超过30分钟,请帮我,如果有在代码中的一些问题。

import thread 
import time 

# Define a function for the thread 
def print_time(threadName, delay): 
    count = 0 
    while count < 5: 
     time.sleep(delay) 
     count += 1 
     print "%s: %s" % (threadName, time.ctime(time.time())) 

    # Create two threads as follows 
try: 
    thread.start_new_thread(print_time, ("Thread-1", 2,)) 
    thread.start_new_thread(print_time, ("Thread-2", 4,)) 
except: 
    print "Error: unable to start thread" 

while 1: 
    pass 

回答

1

删除最后一个代码:

while 1: 
    pass 

它使你的代码运行下去,那么你的IDE将不响应了。 如果您想等待这些线程直到运行结束,最后可以添加time.sleep(35)以等待。

+0

谢谢,它删除了最后一个代码后 – user2439245

1

程序永远不会结束。 您的程序会为每个线程写入5次数据,然后永远挂在主线程上。