Replies: 1 comment
-
Just realized that I never actually gave my opinion here I personally think that this would be a nice addition to hikari, with the unfortunate need for a (partial) rewrite. For the foreseeable future, I don't think this is something I will be looking to try and implement, as I rather focus on stabilizing what we have and building on top of it. This does not mean that it might not get done, I do find the idea quite interesting and it is now in my radar of things to do if I ever get bored. Thanks for bring it to my attention (and sorry for taking so long to write something, just noticed now that I never answered)! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
These thoughts have been swirling around my head for the past few days and while I'm sure this shouldn't be added, I feel it deserves being written down.
Please read the whole thing and don't post a knee-jerk reaction, kthx.
anyio?
anyio is a wrapper library over either
trio
orasyncio
, offering a interface for structured concurrency.Why?
Well, I really really like SC :)
Beyond that, I believe it composes itself MUCH more nicely than, say,
asyncio
. Here's a somewhat biased example to show some concepts:"But hey! That's more code!"
In my view, it's more composable concepts:
anyio
style API would probably emphasizestream
more, making the stream part decently obvious (in fact dispatching would probably use streams under the hood but I'm not sure)anyio.move_on_after
oranyio.fail_after
can be applied anywhere. If someone wants to make anything have a timeout, this will work on it -- unless it usesasyncio.spawn
:-)await stream.next()
is a bit annoying, but oh well.Another possibly more convincing reason is that
asyncio.TaskGroup
is planned (AFAIK) which will bring some aspects of SC tohikari
, so some design changes would be nice nonetheless.One final reason to use
anyio
is that it allows people to use alternative event loops (currently only trio) with hikari.Why not?
I can think of two major reasons: dependencies and performance.
The dependency problem
hikari relies on
aiohttp
which is asyncio specific. Well, ouch.It's used for
httpx
exists and in fact is probably better thanaiohttp
's HTTP client --httpx
supports HTTP/2 behind an extra!httpcore
is used to make the connection and thenwsproto
is wired up such that there is a websocket.The performance problem
anyio is basically an indirection layer; it's an extra function call at best which can cause extra overhead. This does not seem to be too big of a problem though as starlette's performance only dropped by 4.5% when doing a switch to anyio.
The bigger problem is the websocket implementation. I don't have any examples to point to, but I believe aiohttp to be highly optimized unlike whatever hikari would throw together. I propose that this be made and benchmarked first if anything is to be done, as I believe this is the main point that determines whether this is OK.
Restructuring
I believe some parts of hikari have to be changed to fit a SC model where you can only spawn in a
TaskGroup
/Nursery
. I'm not familiar enough with the code but essentially anywhere usingasyncio.spawn
would probably have to be rethought.An observation I've noted is that SC prefers memory channels over spawning tasks. I'm not sure what the perf impact is, but memory channels can be turned into an API that spawns tasks quite easily:
Is this a complete document?
No way! I'm certain I'm missing quite a few points for and against and I think I'll be able to answer questions.
Also, there may be some deal-breaking thing that means this support cannot be done. If so, oh well
I'm writing my own library anyways.Beta Was this translation helpful? Give feedback.
All reactions