我在线程的 $ init调用中有以下代码:
self.conn = copy.deepcopy(conn)
self.conn.setblocking(0)
conn 是一个套接字,并作为参数传递给 $ init 每个线程都会收到一个唯一的套接字。在运行方法中,我有:
self.running = True
self.conn.send("Connected")
print self.name, "has a timeout of", self.conn.gettimeout()
while self.running:
try:
now = self.conn.recv(8192)
print "Recieved:", now, "\n\tFrom:", self.name
self.process(now)
except socket.error:
raise
print "hi from", self.name
time.sleep(1)
超时打印为 0.0,但“hi from threadname”仅在收到消息并且永远不会引发异常时打印出来!看起来好像 recv 方法阻塞了,但它为什么要这样做呢?