Hacker News new | past | comments | ask | show | jobs | submit login

(Almost) Every OS kernel has an API to handle timeouts. poll, select, kqueue, epoll etc. just use that.



The word "just" is doing a lot of work there!


You 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.


I 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.


If 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.


What 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.


Places 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


Even Windows has WaitForMultipleObjects() https://learn.microsoft.com/en-us/windows/win32/api/synchapi...




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

Search: