【发布时间】:2015-04-29 15:29:40
【问题描述】:
当回调循环在 Twisted 中运行时,我的印象是反应器仍然能够从服务器发送/接收数据,因为它能够在回调“之间”运行。但是,当我运行下面的脚本时,它完全忽略了 self.transport.write() 行。该服务器只是 Twisted 在其网站上的基本回显服务器。
from twisted.internet import reactor, protocol
from twisted.internet.defer import Deferred
class EchoClient(protocol.Protocol):
deferred = Deferred()
def connectionMade(self):
self.deferred.addCallback(self.doThing)
self.deferred.callback(0)
def doThing(self, _):
print 'xxx'
self.transport.write('Hello, world!')
self.deferred.addCallback(self.doThing)
def dataReceived(self, data):
"As soon as any data is received, write it back."
print "Server said:", data
self.transport.loseConnection()
class EchoFactory(protocol.ClientFactory):
protocol = EchoClient
def main():
f = EchoFactory()
reactor.connectTCP('192.168.0.7', 8000, f) # Correctly connected to my server
reactor.run()
if __name__ == '__main__':
main()
我预计会打印一个,甚至几个“xxx”,然后服务器会发送“Hello, world!”的回显回到我身边,然后更多的'xxx'。相反,我得到的是“xxx”的无限滚动,甚至没有尝试发送“你好,世界!”到服务器。我错过了什么/误解了什么?
【问题讨论】:
标签: python callback twisted deferred