2016-01-20 123 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次數據,然後永遠掛在主線程上。