Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript Web Worker - close() vs terminate()

I'm not totally understanding the difference between the close() vs terminate() methods for Web Workers. I've read the descriptions here and it seems to be doing the same thing?? http://www.w3.org/TR/workers/

When would I use one over the other?

like image 607
John Lee Avatar asked May 28 '15 08:05

John Lee


People also ask

What would be the result of Terminate () function in web workers?

terminate() The terminate() method of the Worker interface immediately terminates the Worker . This does not offer the worker an opportunity to finish its operations; it is stopped at once.

Can a web worker terminate itself?

Web Workers do not have access to the DOM, window object, document object or parent object. They can be stopped using worker. terminate() from the main script or self. close() inside the worker script itself.

How do I stop web worker?

Web Workers don't stop by themselves but the page that started them can stop them by calling terminate() method. worker. terminate(); A terminated Web Worker will no longer respond to messages or perform any additional computations.

How do I destroy webWorker?

You can Terminate a Worker Using Terminate() Method. At times you may want to cancel the task being executed in a worker. To do so you can destroy a Worker by calling its terminate() method.


3 Answers

The close() method is visible inside the worker's scope. The terminate() method is a part of the worker object's interface and can be called "from the outside".

If you create a worker in your main script and want to stop it from that script you should call the terminate() on the worker object. If you want to stop the worker from the worker code (for example as a response to an external message) you should call the close() method.

Source: https://developer.mozilla.org/en-US/docs/Web/API/Web_Workers_API/Using_web_workers#Terminating_a_worker

like image 94
Paweł Chorążyk Avatar answered Oct 18 '22 19:10

Paweł Chorążyk


Other differences when you use web worker in React:

  1. if you use terminate, you just call the api, and it is synchronous
  2. if you use close, you need to call postMessage and send a signal/message so that the worker can kill itself inside its scope. postMessage is NOT synchronous, as far as I know.

The problem of close is that if want to kill the worker when you unmount a component, you need to do it synchronously, otherwise, you may get a warning, especially when you try to clean up things in the ComponentWillUnmount lifecycle hook or useEffect hook (otherwise, there will be multiple zombie web worker instances alive).

The warning looks like:

Warning: Can't call setState (or forceUpdate) on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in the componentWillUnmount method.

OR:

Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it indicates a memory leak in your application. To fix, cancel all subscriptions and asynchronous tasks in a useEffect cleanup function.

like image 3
Yumin Gui Avatar answered Oct 18 '22 21:10

Yumin Gui


Indeed the close() function is visible from inside the scope of the Worker.

terminate() is visible from outside (i.e: the script that calls the worker can shut it down using this function)

TBH this is a little bit confusing at first, but once you implemented you will get used to it

like image 2
Amine ABDALKHALKI Avatar answered Oct 18 '22 19:10

Amine ABDALKHALKI