变更
This commit is contained in:
@@ -0,0 +1,66 @@
|
||||
import asyncio
|
||||
import time
|
||||
|
||||
|
||||
async def func1():
|
||||
print("hellword")
|
||||
await asyncio.sleep(3) # await是挂起,异步操作代码
|
||||
print("hellword1")
|
||||
|
||||
|
||||
async def func2():
|
||||
print("hellword2")
|
||||
await asyncio.sleep(2) # await是挂起,异步操作代码
|
||||
print("hellword3")
|
||||
|
||||
|
||||
async def func3():
|
||||
print("hellword4")
|
||||
await asyncio.sleep(4) # await是挂起,异步操作代码
|
||||
print("hellword5")
|
||||
|
||||
|
||||
async def main():
|
||||
tasks = [asyncio.create_task(func1()),
|
||||
asyncio.create_task(func2()),
|
||||
asyncio.create_task(func3())]
|
||||
await asyncio.wait(tasks)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
t1 = time.time()
|
||||
asyncio.run(main())
|
||||
t2 = time.time()
|
||||
print(t2 - t1)
|
||||
|
||||
# async def func1():
|
||||
# print("hellword")
|
||||
# # time.sleep(3) # 出现同步操作时候,异步会中断
|
||||
# await asyncio.sleep(3)# await是挂起,异步操作代码
|
||||
# print("hellword1")
|
||||
#
|
||||
#
|
||||
# async def func2():
|
||||
# print("hellword2")
|
||||
# # time.sleep(2)
|
||||
# await asyncio.sleep(2)# await是挂起,异步操作代码
|
||||
# print("hellword3")
|
||||
#
|
||||
#
|
||||
# async def func3():
|
||||
# print("hellword4")
|
||||
# # time.sleep(4)
|
||||
# await asyncio.sleep(4)# await是挂起,异步操作代码
|
||||
# print("hellword5")
|
||||
|
||||
|
||||
# if __name__ == '__main__':
|
||||
# f1 = func1()
|
||||
# f2 = func2()
|
||||
# f3 = func3()
|
||||
# t1 = time.time()3
|
||||
# tasks = [f1, f2, f3]
|
||||
# # 一次启动多个任务
|
||||
# asyncio.run(asyncio.wait(tasks))
|
||||
# t2 = time.time()
|
||||
# print(t2-t1)
|
||||
Reference in New Issue
Block a user