1
這是處理套接字中數據的follow-up question。但是,我無法捕獲「stdin closed」事件。這是我現在有:如何監聽龍捲風循環中關閉的stdin事件?
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()
和測試:
$ ./tornado_sockets.py # expect to close stdin
<C-d> # nothing happens
另一項測試:
$ echo expect_stdin_to_be_closed | ./tornado_sockets.py
# nothing happens
我如何可以監聽標準輸入的結束?