Skip to content
Myr

concurrency.py

This file

Path

tests/concurrency.py

46 lines · 1.2 KiB · Python

Last changed by

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

History of this file

Take it

1"""2This module contains concurrency utilities that are only used in tests, thus not3required as part of the ConcurrencyBackend API.4"""56import asyncio7import functools8import typing910import trio1112from httpx.concurrency.asyncio import AsyncioBackend13from httpx.concurrency.trio import TrioBackend141516@functools.singledispatch17async def sleep(backend, seconds: int):18    raise NotImplementedError  # pragma: no cover192021@sleep.register(AsyncioBackend)22async def _sleep_asyncio(backend, seconds: int):23    await asyncio.sleep(seconds)242526@sleep.register(TrioBackend)27async def _sleep_trio(backend, seconds: int):28    await trio.sleep(seconds)293031@functools.singledispatch32async def run_concurrently(backend, *coroutines: typing.Callable[[], typing.Awaitable]):33    raise NotImplementedError  # pragma: no cover343536@run_concurrently.register(AsyncioBackend)37async def _run_concurrently_asyncio(backend, *coroutines):38    coros = (coroutine() for coroutine in coroutines)39    await asyncio.gather(*coros)404142@run_concurrently.register(TrioBackend)43async def _run_concurrently_trio(backend, *coroutines):44    async with trio.open_nursery() as nursery:45        for coroutine in coroutines:46            nursery.start_soon(coroutine)