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?
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.
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.
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.
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.
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
Other differences when you use web worker in React:
terminate
, you just call the api, and it is synchronous
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.
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
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