NODEJS - Call function between worker_threads and parent (or vice versa)

16 views Asked by At
  • Nodejs supports communication between worker and parent using the postMessage method, however it only supports one-way communication.
  • If you want two-way communication by calling a worker function from the parent and waiting to receive the worker's response (or vice versa), you can use the worker-messenger library on npmjs. https://www.npmjs.com/package/worker-messenger
  • very easy to use it, such as:
result = await messenger.callParent('testParentFunc', 'args', timeout)
result = await messenger.callWorker('testFunction', 'args', timeout)

It works by postingMessage and waiting for the response. Hope it is useful to you.

0

There are 0 answers