How to use the anyio.fail_after function in anyio

To help you get started, we’ve selected a few anyio examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github agronholm / anyio / tests / test_taskgroups.py View on Github external
async def test_nested_shield():
    async def killer(scope):
        await wait_all_tasks_blocked()
        await scope.cancel()

    with pytest.raises(TimeoutError):
        async with create_task_group() as tg:
            async with open_cancel_scope() as scope:
                async with open_cancel_scope(shield=True):
                    await tg.spawn(killer, scope)
                    async with fail_after(0.2):
                        await sleep(2)
github agronholm / anyio / tests / test_taskgroups.py View on Github external
async def test_nested_fail_after():
    async def killer(scope):
        await wait_all_tasks_blocked()
        await scope.cancel()

    async with create_task_group() as tg:
        async with open_cancel_scope() as scope:
            async with open_cancel_scope():
                await tg.spawn(killer, scope)
                async with fail_after(1):
                    await sleep(2)
                    pytest.fail('Execution should not reach this point')

                pytest.fail('Execution should not reach this point either')

            pytest.fail('Execution should also not reach this point')

    assert scope.cancel_called
github Fuyukai / curious / curious / core / gateway.py View on Github external
async def heartbeater() -> None:
            while True:
                try:
                    async with anyio.fail_after(heartbeat_interval):
                        await self._stop_heartbeating.wait()
                except TimeoutError:
                    pass
                else:
                    break

                try:
                    await self.send_heartbeat()
                except (WebSocketClosing, WebSocketClosed, WebSocketUnavailable):
                    return
github theelous3 / asks / asks / utils.py View on Github external
async def timeout_manager(timeout, coro, *args):
    try:
        async with fail_after(timeout):
            return await coro(*args)
    except TimeoutError as e:
        raise RequestTimeout from e
github Fuyukai / curious / curious / ext / paginator / __init__.py View on Github external
await self._reaction_queue.put(reaction)
                else:
                    raise ListenerExit
            except TimeoutError:
                raise ListenerExit from None

        # spawn the consumer task first
        self.bot.events.add_temporary_listener("message_reaction_add", consume_reaction)

        # send the stuff we want
        await self.send_current_page()
        await self._add_initial_reactions()

        try:
            while True:
                async with anyio.fail_after(120):
                    reaction = await self._reaction_queue.get()

                if reaction.emoji == self.BUTTON_FORWARD:
                    if self.page < len(self._message_chunks) - 1:
                        self.page += 1
                    else:
                        self.page = 0
                    await self.send_current_page()

                if reaction.emoji == self.BUTTON_BACKWARDS:
                    if self.page > 0:
                        self.page -= 1
                    else:
                        self.page = len(self._message_chunks) - 1

                    await self.send_current_page()
github clamor-py / anysocks / anysocks / client.py View on Github external
try:
            async with anyio.fail_after(connect_timeout):
                websocket = await create_websocket(
                    task_group, url, use_ssl=use_ssl, subprotocols=subprotocols, headers=headers,
                    message_queue_size=message_queue_size, max_message_size=max_message_size
                )
        except TimeoutError:
            raise TimeoutError from None
        except OSError as exception:
            raise HandshakeError from exception

        try:
            await yield_(websocket)
        finally:
            try:
                async with anyio.fail_after(disconnect_timeout):
                    await websocket.close()
            except TimeoutError:
                raise TimeoutError from None
github clamor-py / anysocks / anysocks / client.py View on Github external
Defaults to 60 seconds.
    disconnect_timeout : Optional[float]
        The number of seconds to wait for the connection to wait before timing out
        when closing the connection. Defaults to 60 seconds.

    Raises
    ------
    :exc:`TimeoutError`
        Raised for a connection timeout. See ``connect_timeout`` and ``disconnect_timeout``.
    :exc:`anysocks.exceptions.HandshakeError`
        Raised for any networking errors.
    """

    async with anyio.create_task_group() as task_group:
        try:
            async with anyio.fail_after(connect_timeout):
                websocket = await create_websocket(
                    task_group, url, use_ssl=use_ssl, subprotocols=subprotocols, headers=headers,
                    message_queue_size=message_queue_size, max_message_size=max_message_size
                )
        except TimeoutError:
            raise TimeoutError from None
        except OSError as exception:
            raise HandshakeError from exception

        try:
            await yield_(websocket)
        finally:
            try:
                async with anyio.fail_after(disconnect_timeout):
                    await websocket.close()
            except TimeoutError:
github vxgmichel / aiostream / aiostream / compat.py View on Github external
async def fail_after(*args, **kwargs):
    try:
        async with anyio.fail_after(*args, **kwargs) as value:
            yield value
    except TimeoutError:
        raise timeout_error()