Handling Stdin With Tornado
How to listen for events that happen on stdin in Tornado loop? In particular, in a tornado-system, I want to read from stdin, react on it, and terminate if stdin closes. At the sam
Solution 1:
You can use the add_handler
method on the IOLoop
instance to watch for events on stdin
.
Here's a minimal working example:
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler
import sys
classMainHandler(RequestHandler):
defget(self):
self.finish("foo")
application = Application([
(r"/", MainHandler),
])
defon_stdin(fd, events):
content = fd.readline()
print"received: %s" % content
if __name__ == "__main__":
application.listen(8888)
IOLoop.instance().add_handler(sys.stdin, on_stdin, IOLoop.READ)
IOLoop.instance().start()
Post a Comment for "Handling Stdin With Tornado"