python协程示例

网友投稿 666 2022-08-31

python协程示例

python协程示例

import timeimport asyncioasync def hello(i): print('Hello World:%s' % time.time()) return idef run(): loop = asyncio.get_event_loop() tasks = [] for i in range(5): task = loop.create_task(hello(i)) loop.run_until_complete(task) tasks.append(task.result()) print([task for task in tasks])# gatherdef run_gather(): loop = asyncio.get_event_loop() coros = [] for i in range(5): task = loop.create_task(hello(i)) coros.append(task) loop.run_until_complete(asyncio.gather(*coros)) print([coro.result() for coro in coros])run()run_gather()

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:go语言中的new与make(go语言make和new的区别)
下一篇:深度学习论文: ENet: A Deep Neural Network Architecture for Real-Time Semantic Segmentation及其PyTorch实现
相关文章

 发表评论

暂时没有评论,来抢沙发吧~