[英]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.