V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
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 · 2016-03-07 17:29:29 +08:00 · 2503 次点击
    这是一个创建于 2979 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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 条回复    2016-03-09 16:05:24 +08:00
    yangtukun1412
        1
    yangtukun1412  
       2016-03-07 17:39:21 +08:00   ❤️ 1
    在同步场景下会自动调用 self.finish(),你可以试试加上 tornado.web.asynchronous 装饰器,再去掉 self.flush() 运行
    calease
        2
    calease  
       2016-03-08 12:21:26 +08:00
    用不用 async 都不用 flush
    如果返回大文件要经常 flush
    不然客户端得等到你所有数据都写入完毕了才能开始下载
    eightqueen
        3
    eightqueen  
    OP
       2016-03-08 15:53:52 +08:00
    @calease 还是不明白为什么不用 flush ?不 flush ,数据仍然在缓冲区,客户端怎么可能收到?
    calease
        4
    calease  
       2016-03-09 00:37:31 +08:00 via iPhone
    @eightqueen
    handler 运行结束时 tornado 会自动帮你 finish ,如果你没有手动 finish 过。 finish 的时候会调用 flush 把所有写的内容提交。
    我第一句只是说 async 和 flush 没有关联,不是 async 了就一定要手动 flush 。
    eightqueen
        5
    eightqueen  
    OP
       2016-03-09 16:05:24 +08:00
    @calease 谢谢
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2023 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 24ms · UTC 01:40 · PVG 09:40 · LAX 18:40 · JFK 21:40
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.