def __init__(self, base_location=None, endpoint=None, headers=None, encoding='utf-8', persist_cookies=None, ssl_context=None, connections=1): ''' Args: encoding (str): The encoding asks'll try to use on response bodies. persist_cookies (bool): Passing True turns on browserishlike stateful cookie behaviour, returning cookies to the host when appropriate. connections (int): The max number of concurrent connections to the host asks will allow its self to have. The default number of connections is 1. You may increase this value as you see fit. ''' super().__init__(headers, ssl_context) self.encoding = encoding self.base_location = base_location self.endpoint = endpoint if persist_cookies is True: self._cookie_tracker = CookieTracker() else: self._cookie_tracker = persist_cookies self._conn_pool = SocketQ() self._sema = create_semaphore(connections)
async def test_semaphore(self): async def acquire(): async with semaphore: assert semaphore.value in (0, 1) semaphore = create_semaphore(2) async with create_task_group() as tg: await tg.spawn(acquire, name='task 1') await tg.spawn(acquire, name='task 2') assert semaphore.value == 2
def __init__(self, listener_name, server_instance, max_connections=-1): self.logger = logging.getLogger(__name__) self.instance = server_instance self.conn_count = 0 self.listener_name = listener_name self.max_connections = max_connections if self.max_connections > 0: self.semaphore = anyio.create_semaphore(self.max_connections) else: self.semaphore = None
def __init__(self, host: str, port: int, pool_size: int = 12, *, connection_factory: 'Callable[[], Redis]' = None): self._host = host self._port = port self._pool_size = pool_size self._connection_factory = connection_factory or create_redis self._sema = anyio.create_semaphore(pool_size) self._connections = collections.deque() self._closed = False
async def test_manual_acquire(self): async def acquire(): await semaphore.acquire() try: assert semaphore.value in (0, 1) finally: semaphore.release() semaphore = create_semaphore(2) async with create_task_group() as tg: tg.spawn(acquire, name='task 1') tg.spawn(acquire, name='task 2') assert semaphore.value == 2
async def test_acquire_cancel(self): async def task(): nonlocal local_scope, acquired async with open_cancel_scope() as local_scope, semaphore: acquired = True local_scope = acquired = None semaphore = create_semaphore(1) async with create_task_group() as tg: async with semaphore: await tg.spawn(task) await wait_all_tasks_blocked() await local_scope.cancel() assert not acquired
async def test_statistics(self): async def waiter(): async with semaphore: pass semaphore = create_semaphore(1) async with create_task_group() as tg: assert semaphore.statistics().tasks_waiting == 0 async with semaphore: assert semaphore.statistics().tasks_waiting == 0 for i in range(1, 3): tg.spawn(waiter) await wait_all_tasks_blocked() assert semaphore.statistics().tasks_waiting == i assert semaphore.statistics().tasks_waiting == 0
async def test_max_value_exceeded(self): semaphore = create_semaphore(1, max_value=2) semaphore.release() pytest.raises(ValueError, semaphore.release)
async def test_max_value(self, max_value): semaphore = create_semaphore(0, max_value=max_value) assert semaphore.max_value == max_value
async def main(): semaphore = create_semaphore(10) async with create_task_group() as tg: for num in range(10): await tg.spawn(use_resource, num, semaphore)
def __init__(self, max_at_once: int) -> None: self.semaphore = anyio.create_semaphore(max_at_once)
def semaphore(self) -> anyio.abc.Semaphore: if not hasattr(self, "_semaphore"): self._semaphore = create_semaphore(self.max_value) return self._semaphore
def __post_init__(self): self.pool = anyio.create_semaphore(self.processor.pool_size)
def create_semaphore(self, max_size: int) -> anyio.Semaphore: return anyio.create_semaphore(max_size)
async def test_acquire_nowait(self): semaphore = create_semaphore(1) semaphore.acquire_nowait() assert semaphore.value == 0 pytest.raises(WouldBlock, semaphore.acquire_nowait)
def sema(self): if self._sema is None: self._sema = create_semaphore(self._connections) return self._sema
async def test_semaphore_release(self): semaphore = create_semaphore(1) semaphore.acquire_nowait() with pytest.deprecated_call(): await semaphore.release()