擅长:python、mysql、java
<p>不幸的是,<code>select</code>无法帮助您在一个线程中处理<code>stdin</code>和网络事件,因为<code>select</code>无法在Windows上处理流。您需要的是一种无阻塞地读取<code>stdin</code>的方法。您可以使用:</p>
<ol>
<li>用于<code>stdin</code>的额外线程。这应该工作得很好,是做这项工作最简单的方法。如果您只需要等待I/O事件,那么Python线程支持就可以了。</li>
<li>类似于<a href="https://pypi.python.org/pypi/gevent" rel="nofollow">gevent</a>中的<a href="https://pypi.python.org/pypi/greenlet" rel="nofollow">greenlet</a>机制,用于修补线程支持的标准库的大多数I/O函数,以防止它们阻塞greenlet。也有类似<code>twisted</code>(请参阅注释)的库提供非阻塞文件I/O。这种方式是最一致的,但它应该要求使用与框架匹配的样式来编写整个应用程序(<code>twisted</code>或<code>gevent</code>,差异不显著)。但是,我怀疑<code>twisted</code>包装器不能从Windows上的<code>stdin</code>异步输入(非常肯定它们可以在*nix上执行,因为它们可能使用相同的<code>select</code>)。</li>
<li>还有别的把戏。然而,大多数可能的伎俩都相当丑陋。</li>
</ol>