Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

This seems to "busy wait" when sleeping, i.e. the event loop keeps running even if nothing is currently runnable. I remember reading about another toy implementation which handled it correctly (the way I believe it's actually done in asyncio) by tracking the next-runnable times of a task in sorted order, and if nothing is currently runnable you can sleep the event loop. And then this was extended so that instead of just next-runnable depending on wall-clock time, the task could have a dependency on a socket or something, so that you can use select w/timeout.


In asyncio itself you can have custom event loop implementations in addition to the out of the box one.

One popular implementation is uvloop (https://github.com/MagicStack/uvloop) which basically just implements the loop using libuv which takes care of doing stuff like `select` as you describe.


I've encountered bugs with it. Unix sockets not being created at all. So I went back to the regular loop.


This sounds a lot like SimPy [1] / Simpy.io [2], but SimPy predates asycio by many years.

[1] https://simpy.readthedocs.io/en/latest/

[2] https://gitlab.com/team-simpy/simpy.io


There's nothing really wrong with this on one level. Your event loop doesn't even have to loop, it can just start by running your main and end when that ends. Imagine starting a server which has a while true loop that waits over a socket, and on some shutdown condition or interrupt ends, ending the program. No busy waiting, no messing with sleeps or sockets from the event loops perspective. Run until complete vs run forever.

Better off, if making some toy loop, to not bother with the forever case.


how do you wait for a socket if the event loop doesn't support waiting for sockets?


Whats the best book to learn about things like these?




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: