After a long discussion thread on discourse, I thought I should setup a draft package to fill the need for a timeout function and macro.
I spent some thoughts and trial and error on this seemingly simple functionality and finally ended up with the code of this package to provide killtask(), timeout()and @timeout.
There’s a package draft Timeout.jl by @ararslan that has never been released. It addresses tasks that are spawned via Distributed.jl. Unfortunately, it currently does not precompile without adaptations. However, I took some inspiration from his code. Particularly the idea of communication via channels was very appealing to me.
The final code is longer than I previously expected, but that's also the reason why I think this package should exist somewhere.
These versions of timeout() and @timeout will not block when tasks cannot be stopped, at least if multiple threads are available.
The killtask() is meant to replace the many @async Base.throwto(task, InterruptException()) that are still found in the wild... This version hopefully never throws and should be sage against race conditions.
I’ve setup this draft repository TimeOut.jl to have the community help to make this a viable package. The code could as well go into a different more visible package, e.g. ThreadPools. Alternatively it could live on its own and enhance methods from ThreadPools with a timeout parameter via extensions. In any case, I shouldn't be the (only) maintainer.
So far my kind of lengthy thoughts …
Happy to receive your thoughts on discourse or here.