Is it possible to cancel the handling of a single action invoked by worker.postMessage(..)
?
There is the worker.terminate()
method, but it cancels any waiting event. Also it seems to render the whole worker dead, as it doesn't react to further postMessage
calls.
On the other hand, it seems useless to create a new Worker
for every task, if they operate on a large amount of common data, that have to be traversed from Worker
to Worker
via the main script, cloning the whole data everytime.
It's true that worker.terminate()
kills the thread. As there's a decent amount of memory and CPU overhead associated with WebWorkers, that fact can be expensive.
The way to handle the canceling of single events would to host some sort of controller logic inside of your postMessage
callback. Your worker can have state, so you'd have to keep track of that state internally and stop or start processes using that semaphore.
One way might be to created a shared Worker, which can even be used across tabs, and then have that shared worker handle spinning off your other threads where necessary.
you’ll need to write the code in your worker if flag x is set via postmessage to ’pause’ specific processing jobs until via postmessage the greenlight is given, the you need to give your code a kickstart to start processing again.
to bad javascript doesn’t support pointers, otherwiseyou wouldn’t have the memory problem.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With