-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathasync_utils.py
More file actions
55 lines (40 loc) · 1.85 KB
/
async_utils.py
File metadata and controls
55 lines (40 loc) · 1.85 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import asyncio
from abc import ABC, abstractmethod
from collections.abc import Awaitable, Callable, Coroutine
from typing import Any
def sync_to_async_func[R](sync_func: Callable[..., R]) -> Callable[..., Awaitable[R]]:
"""Convert a synchronous callable into an asynchronous callable."""
async def wrapper(*args: object, **kwargs: object) -> R:
return await asyncio.to_thread(sync_func, *args, **kwargs)
wrapper.__name__ = sync_func.__name__
wrapper.__doc__ = sync_func.__doc__
return wrapper
def async_to_sync_func[R](async_func: Callable[..., Coroutine[Any, Any, R]]) -> Callable[..., R]:
"""Convert an asynchronous callable into a synchronous callable."""
def wrapper(*args: object, **kwargs: object) -> R:
return asyncio.run(async_func(*args, **kwargs))
wrapper.__name__ = async_func.__name__
wrapper.__doc__ = async_func.__doc__
return wrapper
async def run_async_function_with_semaphore[R](
async_func: Callable[..., Awaitable[R]],
concurrency_sema: asyncio.Semaphore | None,
*args: object,
**kwargs: object,
) -> R:
"""Execute async_func with an optional semaphore limiting concurrency."""
if concurrency_sema is not None:
async with concurrency_sema:
return await async_func(*args, **kwargs)
return await async_func(*args, **kwargs)
class AsyncResource[R](ABC):
"""Base class for async resources protected by a semaphore."""
def __init__(self, concurrency: int = 1) -> None:
self.semaphore = asyncio.Semaphore(concurrency)
async def task(self, *args: object, **kwargs: object) -> R:
async with self.semaphore:
return await self.call(*args, **kwargs)
@abstractmethod
async def call(self, *args: object, **kwargs: object) -> R:
"""Execute the concrete asynchronous operation."""
raise NotImplementedError