来自asyncio.Semaphore的CancelledError,何时发生?

时间:2019-10-04 14:54:12

标签: python concurrency semaphore python-asyncio

我正在运行具有高度并发性的服务,并且在代码的一个位置使用了File xxx.py async with my_semaphore: File /usr/local/lib/python3.7/asyncio/locks.py, line 92, in __aenter__ await self.acquire() File /usr/local/lib/python3.7/asyncio/locks.py, line 474, in acquire await fut concurrent.futures._base.CancelledError 锁。我看到很多像这样的CancelledErrors:

async def acquire(self):
    """Acquire a semaphore.
    If the internal counter is larger than zero on entry,
    decrement it by one and return True immediately.  If it is
    zero on entry, block, waiting until some other coroutine has
    called release() to make it larger than 0, and then return
    True.
    """
    while self._value <= 0:
        fut = self._loop.create_future()
        self._waiters.append(fut)
        try:
            await fut
        except:
            # See the similar code in Queue.get.
            fut.cancel()
            if self._value > 0 and not fut.cancelled():
                self._wake_up_next()
            raise
    self._value -= 1
    return True

检查源代码后,似乎在等待将来充当锁的同时触发异常时发生。

来自asyncio/locks.py:L483-L504

__exit__

我对这段代码感到非常困惑。为什么这样做是必要的,在什么情况下等待未来会出现例外?上下文管理器的innodb_thread_concurrency=0 # from 33 to allow OS management of limit thread_cache_size=100 # from 10 per v7 refman CAP recommended will reduce threads_create count innodb_buffer_pool_size=8G # from 2G for half of your RAM max innodb_lru_scan_depth=100 # from 1024 to conserve 90% CPU cycles used for function 方法捕获的异常不是吗?我正试图了解如何摆脱这些错误。

编辑:这些错误是否可能是由于系统内存不足而引起的?

0 个答案:

没有答案