对单个请求的扭曲多个响应
Twisted multiple responses for single request
我有一个 LineReceiver 协议,想发送两个这样的响应:
def handle_CLIENT(self, data):
...
self.sendLine(r1.raw)
...
self.sendLine(r2.raw)
Twisted 将两个响应合并为一个,就像 Multiple responses in Twisted 中那样。客户端是专有的,我无法改变它的行为。使它起作用的正确方法是什么?谢谢。
编辑
def handle_CLIENT(self, data):
...
self.sendLine(r1.raw)
reactor.doIteration(0.5)
...
self.sendLine(r2.raw)
这对我有用,但我想这不是正确的方法。因为我不知道什么时候会有超过一个客户:)
这绝对不是正确的方法。除其他事项外,根据发生的情况,doIteration
可能会导致无限循环或崩溃。
您要使用的是 callLater
,它可以让您 运行 将来某个时间的功能。
你试图做的是将两个响应强制到两个单独的 TCP 段中,因为你的对等点有问题。有关原因的解释,请参阅 this Twisted FAQ。请注意,这在一般情况下无法工作,并且您的专有客户端刚刚损坏。
但是,您可以通过这样做使其在 大部分时间 工作:
def handle_CLIENT(self, data):
self.transport.pauseProducing()
def sendFirstLine():
self.sendLine(r1.raw)
def sendSecondLine():
self.sendLine(r2.raw)
# now that we've sent the second line we can accept more requests; if
# we accept requests in the middle, we might send responses interleaved
# with each other, which will probably break your client
self.transport.resumeProducing()
reactor.callLater(0, sendFirstLine)
reactor.callLater(0.5, sendSecondLine)
同样,TCP 是一种字节流协议,即使有半秒的延迟,慢速网络 也可能 导致您的两个响应在网络中的某个路由器上粘在一起介于你和你的客户之间。你不能依赖这个。但这可能足以让你摆脱当前的困境——而且这比调用 doIteration
并使你的服务器崩溃要好得多:)
我有一个 LineReceiver 协议,想发送两个这样的响应:
def handle_CLIENT(self, data):
...
self.sendLine(r1.raw)
...
self.sendLine(r2.raw)
Twisted 将两个响应合并为一个,就像 Multiple responses in Twisted 中那样。客户端是专有的,我无法改变它的行为。使它起作用的正确方法是什么?谢谢。
编辑
def handle_CLIENT(self, data):
...
self.sendLine(r1.raw)
reactor.doIteration(0.5)
...
self.sendLine(r2.raw)
这对我有用,但我想这不是正确的方法。因为我不知道什么时候会有超过一个客户:)
这绝对不是正确的方法。除其他事项外,根据发生的情况,doIteration
可能会导致无限循环或崩溃。
您要使用的是 callLater
,它可以让您 运行 将来某个时间的功能。
你试图做的是将两个响应强制到两个单独的 TCP 段中,因为你的对等点有问题。有关原因的解释,请参阅 this Twisted FAQ。请注意,这在一般情况下无法工作,并且您的专有客户端刚刚损坏。
但是,您可以通过这样做使其在 大部分时间 工作:
def handle_CLIENT(self, data):
self.transport.pauseProducing()
def sendFirstLine():
self.sendLine(r1.raw)
def sendSecondLine():
self.sendLine(r2.raw)
# now that we've sent the second line we can accept more requests; if
# we accept requests in the middle, we might send responses interleaved
# with each other, which will probably break your client
self.transport.resumeProducing()
reactor.callLater(0, sendFirstLine)
reactor.callLater(0.5, sendSecondLine)
同样,TCP 是一种字节流协议,即使有半秒的延迟,慢速网络 也可能 导致您的两个响应在网络中的某个路由器上粘在一起介于你和你的客户之间。你不能依赖这个。但这可能足以让你摆脱当前的困境——而且这比调用 doIteration
并使你的服务器崩溃要好得多:)