2019-08-16 14:31:12 +08:00
|
|
|
import random
|
2019-07-29 09:59:10 +08:00
|
|
|
import asyncio
|
2023-12-17 12:21:17 +08:00
|
|
|
import inspect
|
2019-07-29 09:59:10 +08:00
|
|
|
from functools import wraps
|
|
|
|
|
2023-12-17 12:21:17 +08:00
|
|
|
def to_func(func):
|
2019-07-29 09:59:10 +08:00
|
|
|
@wraps(func)
|
|
|
|
def wraped_func(*args,**kw):
|
2023-12-17 12:21:17 +08:00
|
|
|
if inspect.iscoroutinefunction(func):
|
|
|
|
task = asyncio.ensure_future(func(*args,**kw))
|
|
|
|
ret = asyncio.gather(task)
|
|
|
|
return ret
|
|
|
|
return func(*args, **kw)
|
2019-07-29 09:59:10 +08:00
|
|
|
return wraped_func
|
|
|
|
|
2023-12-17 12:21:17 +08:00
|
|
|
class AsyncWorker:
|
2019-08-16 14:31:12 +08:00
|
|
|
def __init__(self,maxtask=50):
|
|
|
|
self.semaphore = asyncio.Semaphore(maxtask)
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
async def __call__(self,callee,*args,**kw):
|
|
|
|
async with self.semaphore:
|
2023-12-17 12:21:17 +08:00
|
|
|
if inspect.iscoroutinefunction(callee):
|
|
|
|
return await callee(*args,**kw)
|
|
|
|
return callee(*args, **kw)
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
async def run(self,cmd):
|
|
|
|
async with self.semaphore:
|
|
|
|
proc = await asyncio.create_subprocess_shell(cmd,
|
|
|
|
stdout=asyncio.subprocess.PIPE,
|
|
|
|
stderr=asyncio.subprocess.PIPE)
|
|
|
|
|
|
|
|
stdout, stderr = await proc.comunicate()
|
|
|
|
return stdout, stderr
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
2023-12-17 12:21:17 +08:00
|
|
|
def hello(cnt,greeting):
|
2019-08-16 14:31:12 +08:00
|
|
|
t = random.randint(1,10)
|
2023-12-17 12:21:17 +08:00
|
|
|
# await asyncio.sleep(t)
|
2019-08-16 14:31:12 +08:00
|
|
|
print(cnt,'cost ',t,'seconds to',greeting)
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
async def run():
|
2023-12-17 12:21:17 +08:00
|
|
|
w = AsyncWorker()
|
|
|
|
g = [ asyncio.create_task(w(hello,i,'hello world')) for i in range(1000) ]
|
|
|
|
await asyncio.wait(g)
|
|
|
|
print('aaaaaaaaaaaaaaaaaaa')
|
2019-07-29 09:59:10 +08:00
|
|
|
|
|
|
|
loop = asyncio.get_event_loop()
|
|
|
|
loop.run_until_complete(run())
|
|
|
|
|