【发布时间】:2011-10-16 00:42:00
【问题描述】:
我在线程的 $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 方法阻塞了,但它为什么会那样做呢?
【问题讨论】:
-
你是从 IDLE 运行这个吗?
-
如果每个线程都有一个唯一的套接字,你为什么要复制?
-
@agf,我想确保引用正确无误。
标签: python sockets blocking nonblocking recv