Python 异步

作者: Sunnky | 来源:发表于2018-10-16 16:41 被阅读11次
    import time
    import asyncio
    import types
    
    
    async def fetch_coroutine():
        print("start")
        response = await _sleep()
        print("end")
        return response
    
    
    async def _sleep():
        for i in range(3):
            print(i)
            await sleep()
        return True
    
    
    @types.coroutine
    def sleep():
        yield time.sleep(1)
    
    
    if __name__ == '__main__':
        loop = asyncio.get_event_loop()
        tasks = [
            fetch_coroutine(),
            fetch_coroutine(),
        ]
        loop.run_until_complete(asyncio.wait(tasks))
        print('All fib finished.')
        loop.close()
    
    

    相关文章

      网友评论

        本文标题:Python 异步

        本文链接:https://www.haomeiwen.com/subject/lsdqoftx.html