• 请不要在回答技术问题时复制粘贴 AI 生成的内容
ansheng
V2EX  ›  程序员

如何学习 Tornado 异步非阻塞?

  •  
  •   ansheng ·
    anshengme · Dec 6, 2016 · 4224 views
    This topic created in 3622 days ago, the information mentioned may be changed or developed.

    表示昨天找了很多文章,都说了些怎么用异步,却没有提到异步的精髓,比如future,@tornado.web.asynchronous和@gen.coroutine装饰器,搞得现在都不知道从哪儿开始看了,看过这部分源码的提个建议呗。

    20 replies  •  2016-12-07 10:26:38 +08:00
    wuxc
    wuxc
    Dec 6, 2016
    1
    从第一个版本开始看,还是挺好懂的。 https://github.com/tornadoweb/tornado
    顺便安利下我用为了看这个的写的个小工具 https://github.com/wuxc/gitjump
    Ahri
    Ahri
    Dec 6, 2016
    2
    这个已经过时了,现在都用 asyncio 。
    ericls
    ericls
    Dec 6, 2016
    3
    手动写一个 event loop 来实现同样的效果
    ericls
    ericls
    Dec 6, 2016
    4
    @Ahri asyncio 提供的是一个 api 和一个默认的 event loop
    smartdie
    smartdie
    Dec 6, 2016
    5
    先了解 python 的协程, yield 关键字
    再看 @gen.coroutine 的源码就好理解了
    ansheng
    ansheng
    OP
    Dec 6, 2016
    6
    @wuxc 官方文档一篇篇看??
    ansheng
    ansheng
    OP
    Dec 6, 2016
    7
    @smartdie thx,现在就在看 @gen.coroutine 了,主要是不知道应该从哪儿看起,网上文档太乱了。
    zhuangzhuang1988
    zhuangzhuang1988
    Dec 6, 2016
    8
    future 这个最好看别的语言的, C#的 task, java 的 Future
    @tornado.web.asynchronous 记得开始的时候这个就是简单个标志, 让函数结束的时候不关闭 socket, 可以继续传输数据
    @gen.coroutine 这个看 http://aosabook.org/en/500L/a-web-crawler-with-asyncio-coroutines.html http://www.dabeaz.com/coroutines/index.html 这 2 个
    ansheng
    ansheng
    OP
    Dec 6, 2016
    9
    @zhuangzhuang1988 英文能力太差。
    p2p
    p2p
    Dec 6, 2016
    10
    阅读下 tornado 2.* 版本的异步实现的源码
    sujin190
    sujin190
    Dec 6, 2016❤️1
    11
    Future 简单来说就是一个一个的状态,一连串的 Future 就构成了整个的调用链,类比于函数调用链,区别只是函数是用栈来保存转态的, event loop 监听 io 变更转化为事件通过 yield 的关键字产生的入口变更 Future 状态的变更,转而又引发一连串 Future 状态变更, Future 状态的变更又会产生 io 操作,整个循环就完成了。
    zenliver
    zenliver
    Dec 6, 2016
    12
    ioloop, iostream, gen 看完这 3 个模块源码, 估计用 tornado 不会遇到 hold 不住的问题了
    ansheng
    ansheng
    OP
    Dec 6, 2016
    13
    @zenliver 真的假的
    ansheng
    ansheng
    OP
    Dec 6, 2016
    14
    @sujin190 感觉水好深,
    fy
    fy
    Dec 6, 2016
    15
    看官方文档和 examples ,同时看最新的。
    这个也是改过几次了,看网上文章基本不靠谱的,各种时间段都有,还来回转载。
    jy01264313
    jy01264313
    Dec 6, 2016
    16
    推荐看一下 POSIX 标准 IO
    ansheng
    ansheng
    OP
    Dec 6, 2016
    17
    @fy 如果只是怎么用异步,这个问题倒是不大,主要是他怎么实现异步的,这个就需要特别深入了
    songco
    songco
    Dec 6, 2016
    19
    这块比较简单的我记得是 redis 的网络模块(我当年看的老版本, 新版本应该变化不大).... c 语言的, 有个好处是比较接近底层
    HFcbyqP0iVO5KM05
    HFcbyqP0iVO5KM05
    Dec 7, 2016 via Android
    20
    500 Lines 里面有一个 IO 异步爬虫的实现,把那个读一下可以了解一些基本的东西
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   969 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 42ms · UTC 19:29 · PVG 03:29 · LAX 11:29 · JFK 14:29
    ♥ Do have faith in what you're doing.