Twisted客户端无法将在Windows上以不同方式工作的客户端连接到Linux/M

2024-09-30 00:34:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在使用Twisted endpoint connect()函数时检测失败的连接。奇怪的是,下面的语句在Windows下工作,并给出了预期的结果,但在Linux/macos系统上,我从来没有看到errBack的print语句。是我的代码不正确还是Windows Twisted的工作方式与其他的不同?你知道吗

class Gateway():
    def __init__(self):

        from twisted.internet.endpoints import TCP4ClientEndpoint
        endpoint = TCP4ClientEndpoint(reactor, 'localhost', 8000)
        authInterfaceFactory = AuthInterfaceFactory(self.__authMsgProcessor)
        d = endpoint.connect(authInterfaceFactory)
        d.addErrback(self.ConnFailed)
        print("WAITING...")

    def ConnFailed(self, msg):
        print("[DEBUG] Errback : {0}".format(msg))

Windows结果

WAITING... [DEBUG] Errback : [Failure instance: Traceback (failure with no frames): : Connection was refused by other side: 10061: No connection could be made because the target machine actively refused it..]

我创建了一个使用endpoint connect的客户端,它会立即返回,尽管在与我的代码相同的设置中使用它时,它不会:

    self.__networkThread = threading.Thread(target=reactor.run,
        kwargs={"installSignalHandlers": False})
    self.__networkThread.start()

    from twisted.internet.endpoints import TCP4ClientEndpoint
    endpoint = TCP4ClientEndpoint(reactor, 'localhost', 8000)
    d = endpoint.connect(authInterfaceFactory)
    d.addErrback(self.ConnFailed)
    d.addCallback(self.ConnOK)

在线程中运行reactor时,逻辑是否不正确(我必须这样做,因为我希望它从一开始就启动)?你知道吗


Tags: 代码fromselfwindowsdefconnecttwisted语句
1条回答
网友
1楼 · 发布于 2024-09-30 00:34:32

不能在一个线程中运行reactor,而在另一个线程中使用Twisted api。除了几个专门用于与线程交互的api之外,还必须使用来自单个线程的所有Twisted api。你知道吗

“我希望它从一开始就开始”听起来不像是使用线程的理由。许多使用Twisted的程序在没有线程的情况下“一开始”就启动了reactor。你知道吗

(也请将此作为需要完整示例的一个极好示例。)

相关问题 更多 >

    热门问题