擅长:python、mysql、java
<p>如果<code>msgQ</code>为空,则对<code>self.msgQ.get()</code>的调用将引发<code>Empty</code>异常,并完全跳过对<code>self.sock.send()</code>和{<cd5>}的调用。您的<code>Empty</code>异常的异常处理程序不执行任何操作,因此您的代码将<a href="http://en.wikipedia.org/wiki/Busy_wait" rel="nofollow">busy wait</a>,直到出现在<code>msgQ</code>中,而不会调用<code>send</code>或{<cd9>}。在</p>
<p>一种可能的解决方案是使用python<a href="http://docs.python.org/library/select.html" rel="nofollow">select module</a>检查异常处理程序中的套接字。大致如下:</p>
<pre><code>def run(self):
while not self._terminate:
try:
msgs = self.msgQ.get()
self.sock.send(pickle.dumps(msgs))
rdy = pickle.loads(self.sock.recv(2097152))
except socket.error, EOFError:
print 'log socketmanager closing'
self.terminate()
break
except Empty:
results = select.select([], [], [self.sock], 0.5) # timeout of 0.5 seconds
if self.sock in results[2]:
print 'exceptional condition on socket'
self.terminate()
break
</code></pre>