Git Product home page Git Product logo

coroutine-pure's Introduction

协程往事

根据asyncio制作了asynciox,并提供sleep方法,尽量不修改源代码的结构,asynciox支持本地运行和调试

温馨提示

如果有对协程基础不理解的, 请参看 协程系列汇总

  1. 协程-yield

  2. 协程-yield from

  3. 协程-async

  4. 协程-await

  5. 协程-yield让出控制权

  6. 协程-async让出控制权

  7. 协程-yield from并发

  8. 协程-await并发

  9. 协程-asyncio例子

start

python main.py

asynciox使用

from asynciox.base_eventx import BaseEventLoop
from asynciox.taskx import gather, sleep

async def req1(loop):
    await sleep(1, loop=loop)
    return 1
async def req2():
    return 2

async def main(loop):
    res = await gather(req1(loop), req2(), loop=loop)
    print(res)

if __name__ == '__main__':
    loop = BaseEventLoop()
    BaseEventLoop().run_until_complete(main(loop))

简述

执行顺序: 
1.初始:loop.run_until_complete(main(loop))
   1.1.ensure_future: 把main协程放到_ready队列, 
   1.2.run_forever: 本文中把这个方法称作循环器, 会调用main协程的send方法
2.收集: async def main(loop)
    2.1 await gather(req1(loop), req2(), loop=loop): gather方法会创建一个新的对象_GatheringFuture,并将gather参数中的所有协程都放到_ready队列(此列中是req1和req2协程), 然后,暂停在_GatheringFuture对象的__iter__方法
3.暂停: 循环器分别调用req1和req2协程的send方法, 最终解除_GatheringFuture对象的__iter__方法中的暂停
4.回调: req2调用回调, 最后一个协程req1调用回调, 触发_GatheringFuture的回调
5.解除暂停: _GatheringFuture对象的__iter__方法

coroutine-pure's People

Contributors

xiaoxinmiao avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.