apppublic/appPublic/worker.py

75 lines
1.9 KiB
Python
Raw Normal View History

2024-02-21 15:24:23 +08:00
import time
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
2024-02-03 12:04:38 +08:00
from functools import wraps
def awaitify(sync_func):
"""Wrap a synchronous callable to allow ``await``'ing it"""
@wraps(sync_func)
2024-08-07 11:33:54 +08:00
async def async_func(*args, **kw):
2024-02-21 16:51:26 +08:00
loop = asyncio.get_event_loop()
2024-08-07 11:33:54 +08:00
return await loop.run_in_executor(None, sync_func, *args, **kw)
return async_func
def coroutinify(func):
2024-08-07 11:46:33 +08:00
@wraps(func)
2024-08-07 11:33:54 +08:00
async def async_func(*args):
loop = asyncio.get_event_loop()
2024-08-07 11:46:33 +08:00
return await loop.run_in_executor(None, func, *args)
2024-02-03 12:04:38 +08:00
return async_func
2019-07-29 09:59:10 +08:00
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)
2024-02-21 15:24:23 +08:00
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)
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()
2024-02-28 17:53:44 +08:00
f = awaitify(hello)
g = [ asyncio.create_task(w(f,i,'hello world')) for i in range(100) ]
2023-12-17 12:21:17 +08:00
await asyncio.wait(g)
print('aaaaaaaaaaaaaaaaaaa')
2019-07-29 09:59:10 +08:00
loop = asyncio.get_event_loop()
loop.run_until_complete(run())