推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
eightqueen
V2EX  ›  Python

tornado 的 flush 方法有啥用啊?

  •  
  •   eightqueen · Mar 7, 2016 · 2998 views
    This topic created in 3722 days ago, the information mentioned may be changed or developed.
    from tornado.ioloop import IOLoop
    import tornado.web
    
    class MainHandler(tornado.web.RequestHandler):
        def get(self):
            self.write("Hello, world")
            self.flush()
    
    def make_app():
        return tornado.web.Application([
            (r"/", MainHandler),
        ])
    
    if __name__ == "__main__":
        app = make_app()
        app.listen(1234)
        IOLoop.current().start()
    

    很简单的一个 hello world 程序,我把 flush 去掉,照样运行正常。

    5 replies    2016-03-09 16:05:24 +08:00
    yangtukun1412
        1
    yangtukun1412  
       Mar 7, 2016   ❤️ 1
    在同步场景下会自动调用 self.finish(),你可以试试加上 tornado.web.asynchronous 装饰器,再去掉 self.flush() 运行
    calease
        2
    calease  
       Mar 8, 2016
    用不用 async 都不用 flush
    如果返回大文件要经常 flush
    不然客户端得等到你所有数据都写入完毕了才能开始下载
    eightqueen
        3
    eightqueen  
    OP
       Mar 8, 2016
    @calease 还是不明白为什么不用 flush ?不 flush ,数据仍然在缓冲区,客户端怎么可能收到?
    calease
        4
    calease  
       Mar 9, 2016 via iPhone
    @eightqueen
    handler 运行结束时 tornado 会自动帮你 finish ,如果你没有手动 finish 过。 finish 的时候会调用 flush 把所有写的内容提交。
    我第一句只是说 async 和 flush 没有关联,不是 async 了就一定要手动 flush 。
    eightqueen
        5
    eightqueen  
    OP
       Mar 9, 2016
    @calease 谢谢
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2830 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 85ms · UTC 15:26 · PVG 23:26 · LAX 08:26 · JFK 11:26
    ♥ Do have faith in what you're doing.