简体   繁体   中英

create worker file (worker threads nodejs)

I want to create a worker to send back an array as response to my main file, but I can't understand why the worker is not sending postMessage to main. Here is some dummy code to explain

My main file:

const { Worker } = require('worker_threads')
const worker = new Worker('./workerFile.js')

worker.on('message', msg => console.log(msg))

My workerFile.js

this.postMessage('hello world!')

I had to import parentPort and then use it in workerFile.js:

const { parentPort } = require('worker_threads')
parentPort.postMessage('hello world!')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM