0
如何在下面的例子中正確使用連接(超時)功能?超時似乎對主線程執行沒有影響。從文檔中,主線程被阻塞,直到線程加入或超時。如何在Python線程中正確使用連接(超時)?
import threading,time
class Server(threading.Thread):
def __init__(self, hostname):
super(Server, self).__init__()
self.__hostname = hostname
def run(self):
print self.__hostname+' left'
time.sleep(5)
print self.__hostname+' back'
sem.release()
#init
sem = threading.BoundedSemaphore(4)
threads = []
for x in xrange(1,5):
sem.acquire()
t = Server('thread '+str(x))
threads.append(t)
t.start()
for t in threads:
t.join(2)
print 'why is this line executed by main thread 5 seconds after, not 2?'
每個加入呼叫都有其自己的2秒超時。 – user2357112
有關解決方案,請參閱:http://stackoverflow.com/q/24065808/2073595 – dano