5 ms·
(Almost) Every OS kernel has an API to handle timeouts. poll, select, kqueue, epoll etc. just use that.
by Asmod4n 2y ago
(Almost) Every OS kernel has an API to handle timeouts. poll, select, kqueue, epoll etc. just use that.
- simonw 2y agoThe word "just" is doing a lot of work there!
- immibis 2y agoYou can replace every blocking call with a timed one. In Python you can just call settimeout on a socket. In C you might want to write your own recv_timeout function, but it's not that difficult.
- simonw 2y agoI have no idea how I would use settimeout with a socket to solve any of my problems that involve causing a piece of code to terminate early if it takes longer than a specific amount of time.
- immibis 2y agoWhat is the code doing? Most of the time it's waiting for a socket. If it's not doing that, do the equivalent for whatever it is waiting for. If it's CPU-bound, add interrupt checks.
- simonw 2y agoPlaces I've wanted to implement this in the past include: - Run a subprocess such as "rg" and terminate early if necessary - Run a SQLite SQL a query that errors if it takes more than a second - Same but for other DBs - MySQL, PostgreSQL, Elastic, Mongo etc - Execute a fragment of JavaScript in something like QuickJS with a time limit - Brute force some kind of algorithm with a time limit - using a library I did not write myself
- Asmod4n 2y agoIf you are using a piece of software which has a part that can take a long time to execute and doesn't allow it to set timeouts for long running operations then i would send a PR to solve that issue. When said software is closed source and doesn't have that feature then that company sells a problem and not a solution.
- ape4 2y agoEven Windows has WaitForMultipleObjects() https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitformultipleobjects https://learn.microsoft.com/en-us/windows/win32/api/synchapi...