from __future__ import annotations import asyncio from collections import defaultdict class FeishuSequentialQueue: def __init__(self): self._queues: dict[str, asyncio.Queue] = {} self._locks: dict[str, asyncio.Lock] = defaultdict(asyncio.Lock) async def acquire(self, key: str) -> None: lock = self._locks[key] await lock.acquire() def release(self, key: str) -> None: lock = self._locks.get(key) if lock and lock.locked(): lock.release() async def run_sequential(self, key: str, coro) -> None: await self.acquire(key) try: await coro finally: self.release(key) def remove(self, key: str) -> None: self._locks.pop(key, None) def clear(self) -> None: self._locks.clear()