andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.04k stars 162 forks source link

[QUESTION] Task execution timeout #363

Open Deadly0 opened 3 years ago

Deadly0 commented 3 years ago

Hi, is it possible to set timeout for task execution by which the task will be forcibly terminated?

andywer commented 3 years ago

Hi! I suppose you're talking about thread pools. No, there is currently no out-of-the-box solution to this.

Do you mean killing the whole worker and spawning a new one if some task runs longer than the timeout? That feels a bit radical… 😅

I wonder if it's not usually a better idea to let the worker make sure to not spend an infinite amount of time on anything it does.

aparshin commented 3 years ago

I have the following use case: I use threads.js inside a Node.js server (based on Express.js) to execute several client requests in parallel. Unfortunately, the complexity of the request is defined by request parameters in a pretty complex way, which doesn't allow me to predict processing time in advance. Moreover, most time the processing in threads happens inside third-party JS libraries, which prevents stopping processing from inside the thread...

So, the only solution for my use case I see is to kill a thread with too long request and spawn a new one. I agree that it's pretty radical behaviour. Also, it leads to some overall performance degradation because of thread spawning overhead.