Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cancel a single Webworker event

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.

like image 952
dronus Avatar asked Jun 14 '12 20:06

dronus


2 Answers

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.

like image 189
buley Avatar answered Sep 19 '22 15:09

buley


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.

like image 21
Tschallacka Avatar answered Sep 20 '22 15:09

Tschallacka