Python Twisted addReader适用于linux但不适用于windows

时间:2012-07-30 23:35:28

标签: python twisted

我正在尝试使用Twisted在python 2.7中编写客户端。我的代码在linux(debian squeeze)中运行得很好,但是当我在windows(xp和7)上尝试它时,我得到了一个不断的错误消息流。这些消息的屏幕截图是here

我已经缩小了这个bug并且能够编写一个非常简化的客户端版本,但仍然包含该错误:

from twisted.internet.protocol import Protocol,ClientFactory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class TheClient(LineReceiver):
    def lineReceived(self,line):
        print line

    def connectionLost(self,reason):
        reactor.stop()

class TheFactory(ClientFactory):
    protocol = TheClient

class Test(object):
    def doRead(self):
        pass

    def fileno(self):
        return 0

    def connectionLost(self,reason):
        print 'connection lost'

    def logPrefix(self):
        return 'Client'

def main():
    print 'starting'
    test = Test()
    reactor.addReader(test)
    reactor.run()

if __name__ == '__main__':
    main()

如果注释掉包含'reactor.addReader(test)'的行,我不会收到任何错误消息。如果我在linux上运行此代码而不注释任何行,我不会收到任何错误消息。

我发现this question,我不认为它是同一个问题,但正如预期的那样,它在Windows上无法正常运行。

这段代码是否正确,这是一个Windows错误,还是我必须以不同的方式为它在Windows中工作?

1 个答案:

答案 0 :(得分:2)

select的Windows实现仅支持套接字。假设您的进程中的文件描述符0不代表套接字。它更可能代表与标准I / O相关的东西。

如果您只是想使用标准I / O,那么twisted.internet.stdio,虽然您可能会在Windows上遇到一些粗糙的边缘(错误报告和修复程序赞赏!)。

如果您对标准I / O不感兴趣而且0只是一个任意测试,您可能需要决定您特别想要做什么样的输入。根据您拥有的文件描述符类型,可能会有不同的方法来成功读取它。