[英]Tornado equivalent to Twisted's dataReceived?
首先使用Twisted,我可能无法以正确的方式使用Tornado解决双向通信问题。
扭曲的数据接收方式是:
class MyProtocol(Protocol):
def dataReceived(self, data):
# Figure out if this is a chunk of a previous message
# or if it's a new message
我正在使用Tornado来执行此操作,这似乎可以工作,但有些不同:
class MyClient(object):
@coroutine
def main_loop(self):
while True:
message_header = yield Task(self.stream.read_bytes, 8)
# Read/write from here
该文档似乎没有建议任何“更清洁”的方法(或者就此而言,没有任何方法),那么我会以这种正确的方式进行操作吗?
等效于IOStream
Twisted的Protocol
类似于stream.read_until_close(callback=self.connectionLost, streaming_callback=self.dataReceived)
。 但是,做第二个示例中的操作,并使用其他读取方法( read_bytes
, read_until
等)来单独读取各个块中的read_until
,这更加惯用了。 请注意,由于独立的close回调, IOStream
当前并不是非常友好的协程,因此最好编写带有显式回调直接与IOStream
代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.