This commit is contained in:
yumoqing 2024-02-21 15:24:23 +08:00
parent b97c946891
commit fb9d2771d1

View File

@ -1,3 +1,4 @@
import time
import random import random
import asyncio import asyncio
import inspect import inspect
@ -43,12 +44,19 @@ class AsyncWorker:
if __name__ == '__main__': if __name__ == '__main__':
def hello(cnt,greeting): def hello(cnt,greeting):
t = random.randint(1,10) t = random.randint(1,10)
# await asyncio.sleep(t) print(cnt,'will sleep ',t,'seconds')
time.sleep(t)
print(cnt,'cost ',t,'seconds to',greeting)
async def ahello(cnt,greeting):
t = random.randint(1,10)
print(cnt,'will sleep ',t,'seconds')
await asyncio.sleep(t)
print(cnt,'cost ',t,'seconds to',greeting) print(cnt,'cost ',t,'seconds to',greeting)
async def run(): async def run():
w = AsyncWorker() w = AsyncWorker()
g = [ asyncio.create_task(w(hello,i,'hello world')) for i in range(1000) ] g = [ asyncio.create_task(w(hello,i,'hello world')) for i in range(100) ]
await asyncio.wait(g) await asyncio.wait(g)
print('aaaaaaaaaaaaaaaaaaa') print('aaaaaaaaaaaaaaaaaaa')