asyncio实现python异步io
程序员文章站
2024-01-28 11:34:10
...
简单实现并发执行
import asyncio
async def hello():
print("Hello world1111!")
#模拟io操作
r = await asyncio.sleep(1)
#r = await asyncio.sleep()
print("11111111111111111")
async def hello_test():
print("Hello world2222!")
r = await asyncio.sleep(2)
print("22222222222222")
#获取EventLoop:
loop = asyncio.get_event_loop()
#执行coroutine
task=[hello(),hello_test()]
tasks = asyncio.wait(task)
loop.run_until_complete(tasks)
loop.close()
上一篇: 32-知识笔记(java-多态)
下一篇: js格式化时间