我试图在使用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))
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时,逻辑是否不正确(我必须这样做,因为我希望它从一开始就启动)?你知道吗
不能在一个线程中运行reactor,而在另一个线程中使用Twisted api。除了几个专门用于与线程交互的api之外,还必须使用来自单个线程的所有Twisted api。你知道吗
“我希望它从一开始就开始”听起来不像是使用线程的理由。许多使用Twisted的程序在没有线程的情况下“一开始”就启动了reactor。你知道吗
(也请将此作为需要完整示例的一个极好示例。)
相关问题 更多 >
编程相关推荐