Python异步编程Async/Await

来源:互联网 发布:淘宝的实名认证在哪里 编辑:程序博客网 时间:2024/05/18 15:04

python 从3.5开始从语言层面提供了新的异步编程语法。

import asyncioasync def hello():    print("hello the world")    r = await asyncio.sleep(1)    print("hello again")def main():    loop = asyncio.get_event_loop()    """    tasks = [        asyncio.ensure_future(hello()),    ]    loop.run_until_complete(asyncio.wait(tasks))    """    print("begin")    loop.run_until_complete(hello())    print("end")    loop.close()    print("program is finished.")if __name__ == "__main__":    main()

0 0
原创粉丝点击