This is a follow-up question on handling the data in the socket. However, I am unable to capture the "stdin closed" event. Here's what I have now:
import sys
import tornado
from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler
class MainHandler(RequestHandler):
def get(self):
self.finish("foo")
application = Application([ (r"/", MainHandler), ])
@tornado.gen.coroutine
def close_callback(*args, **kwargs):
print args, kwargs
if __name__ == "__main__":
application.listen(8888)
stdin = tornado.iostream.PipeIOStream(sys.stdin.fileno())
stdin.set_close_callback(close_callback)
IOLoop.instance().start()
And a test:
$ ./tornado_sockets.py # expect to close stdin
<C-d> # nothing happens
Another test:
$ echo expect_stdin_to_be_closed | ./tornado_sockets.py
# nothing happens
How can I listen for closing of stdin?
Quote sys.stdin does not close on ctrl-d:
So basically you need to assert read line with empty string. Some example without
PipeIOStream
:With
PipeIOStream
it is pretty straightforward usingread_until_close
. Callback will be called on close or on Ctrl+D.