Skip to content

Instantly share code, notes, and snippets.

@davidallsopp
Last active January 26, 2018 02:20
Show Gist options
  • Save davidallsopp/2129ad95f7aeeb68d7df to your computer and use it in GitHub Desktop.
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)
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