cog icon indicating copy to clipboard operation
cog copied to clipboard

Use uvloop for async event loop

Open mattt opened this issue 1 year ago • 5 comments

From https://github.com/MagicStack/uvloop:

uvloop is a fast, drop-in replacement of the built-in asyncio event loop. uvloop is implemented in Cython and uses libuv under the hood.

Hard to argue with 2x speedup for a drop-in replacement. Curious to see whether that holds up.

mattt avatar Jul 31 '24 20:07 mattt

There's a good chance we're already using it implicitly. I just wanted to test this by making it explicit.

mattt avatar Jul 31 '24 21:07 mattt

How well does this combine with customers of our library who use asyncio? Should there maybe be an escape hatch?

yorickvP avatar Aug 01 '24 07:08 yorickvP

@technillogue pointed out that uvloop was already added to the async branch via httpx. Uvicorn advertises that the default "auto" loop setting selects uvloop when available, and I don't see any evidence to the contrary.

@yorickvP That's a good callout, but I don't anticipate a problem. The vast majority of consumers will run cog.http as a self-contained program, and anyone consuming as a library could pretty easily swap out their preferred event loop as desired.

mattt avatar Aug 01 '24 11:08 mattt

The reason I ask is that, with async cog, we saw people trying to combine it with their own event loops in their own threads, and it took a bit of hacking to make that work.

yorickvP avatar Aug 02 '24 16:08 yorickvP

Reopening to apply changes to main, which doesn't (yet) have httpx dependency.

mattt avatar Aug 12 '24 11:08 mattt