ForcePilot/backend/package/yuxi/channel/sdk/async_lock.py

18 lines
433 B
Python
Raw Normal View History

from __future__ import annotations
import asyncio
from collections.abc import Callable, Coroutine
from typing import TypeVar
T = TypeVar("T")
def create_async_lock() -> Callable[[Callable[[], Coroutine[None, None, T]]], Coroutine[None, None, T]]:
lock = asyncio.Lock()
async def with_lock(task: Callable[[], Coroutine[None, None, T]]) -> T:
async with lock:
return await task()
return with_lock