Last active
January 26, 2018 02:20
-
-
Save davidallsopp/2129ad95f7aeeb68d7df to your computer and use it in GitHub Desktop.
Example of reading lines from a socket using Tornado IOStream (via the TCPClient helper)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tornado import gen | |
from tornado.ioloop import IOLoop | |
from tornado.tcpclient import TCPClient | |
stream = None | |
def out(data): | |
print(data) | |
stream.read_until(b"\n", callback=out) | |
@gen.coroutine | |
def setup(): | |
global stream | |
stream = yield TCPClient().connect("localhost", 8000) | |
stream.read_until(b"\n", callback=out) | |
# or could call out() with empty data, to save duplication | |
if __name__ == '__main__': | |
setup() | |
IOLoop.current().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment