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