Auto-detect concurrency backend based on the async environment
See original GitHub issueCurrently, 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 inutils.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 oncurio
, since we don’t have aCurioBackend
.) - Use this utility in all places where we currently default to
AsyncioBackend
as abackend
, e.g.AsyncClient
,ConnectionPool
, etc.
Issue Analytics
- State:
- Created 4 years ago
- Comments:23 (23 by maintainers)
Top 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 >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
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.
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.