Skip to content

Auto-detect concurrency backend based on the async environment #389

@florimondmanca

Description

@florimondmanca

Currently, if users want to run HTTPX on trio they need to manually pass a TrioBackend to the AsyncClient:

import trio
import httpx
from httpx.concurrency.trio import TrioBackend

async def main():
    async with httpx.AsyncClient(backend=TrioBackend()) as client:
        ...

trio.run(main)

On the other hand, running on asyncio doesn't require passing any backend argument, which is a better user experience we'd like to have in all cases.

We should allow AsyncClient() to auto-detect the async environment it's running in, and use the appropriate backend — and make that the default behavior, instead of asyncio.

Implementation ideas:

  • Create a get_default_concurrency_backend() utility in utils.py. Detecting the async library in use can be done using sniffio. If no backend is available, raise an appropriate exception. (For example, this should currently be the case if we tried to run on curio, since we don't have a CurioBackend.)
  • Use this utility in all places where we currently default to AsyncioBackend as a backend, e.g. AsyncClient, ConnectionPool, etc.

Metadata

Metadata

Assignees

No one assigned

    Labels

    user-experienceEnsuring that users have a good experience using the library

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions