question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Auto-detect concurrency backend based on the async environment

See original GitHub issue

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.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:23 (23 by maintainers)

github_iconTop GitHub Comments

2reactions
tomchristiecommented, Nov 7, 2019

Ah s’all good. The important thing from my POV is that we make sure we’re communicating and learning between ourselves - I’m seeing such a positive and constructive attitude on all sides, and yeah I totally dig that sometimes it’s easier to just demonstrate first.

There’s plenty of difficult trade-offs wrt. maturing the async ecosystem in Python - what I’m really appreciating at the moment is that I’m seeing a healthy respect for different folks taking different tacks onto helping be part of that.

2reactions
pquentincommented, Nov 7, 2019

I agree! @njsmith and I should have spent more time explaining why we’re continuing the work on our urllib3 branch, and I hope we’ll be able to do this in the next few days.

Oops! More than one month has passed by. @njsmith told me he would like to write a proper comparison at some point, but that takes time and energy. I certainly find it mentally taxing to explain why I prefer to contribute to project A and not project B, when the two projects have similar goals!

I know Nathaniel has some technical reasons that I would not be able to explain well. So this will only be part of the story, but I can explain why I am working on the urllib3 fork since January 2018: I don’t want to waste the huge amount of real world experience urllib3 has. It seems wasteful to have to relearn this by waiting for a slow trickle of bug reports. In fact much of my work on the fork has been upstreamed (here’s a recent example) and this code now benefits the millions of urllib3 downloads that happen every day. And we continue to merge the changes made in urllib3 itself regularly, and all the sync tests for the core library actually pass, so we’ll soon be able to focus on improving the async support.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async Support - HTTPCore - Encode
Supported environments​​ HTTPX supports either asyncio or trio as an async environment. It will auto-detect which of those two to use as the...
Read more >
Async Support - HTTPX
Async is a concurrency model that is far more efficient than multi-threading, ... It will auto-detect which of those two to use as...
Read more >
Parallelism, concurrency, and async programming in Node.js
Node.js uses an asynchronous event-driven design pattern, which means that multiple actions are taken at the same time while executing a program ...
Read more >
Guides::Cookbook - Cooking with Mojolicious
#NAME. Mojolicious::Guides::Cookbook - Cooking with Mojolicious. #OVERVIEW. This document contains many fun recipes for cooking with Mojolicious. #CONCEPTS.
Read more >
Concurrency, Parallelism, Threads, Processes, Async, and Sync
In a single core environment, concurrency happens with tasks executing over same time period via context switching i.e at a particular time period, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found