worker.postMessageToThread(threadId, value[, transferList][, timeout])
threadId<number> 目标线程ID。如果线程ID无效,将抛出ERR_WORKER_MESSAGING_FAILED错误。如果目标线程ID是当前线程ID,将抛出ERR_WORKER_MESSAGING_SAME_THREAD错误。value<any> 要发送的值。transferList<Object[]> 如果在value中传入一个或多个类似MessagePort的对象,则这些项目需要transferList,否则将抛出ERR_MISSING_MESSAGE_PORT_IN_TRANSFER_LIST。有关更多信息,请参见port.postMessage()。timeout<number> 等待消息以毫秒为单位传递的时间。 默认值是undefined,表示永远等待。如果操作超时,将抛出ERR_WORKER_MESSAGING_TIMEOUT错误。- 返回:<Promise> 如果消息被目标线程成功处理,则该承诺会被履行。
将值发送给另一个由其线程 ID 标识的工作程序。
🌐 Sends a value to another worker, identified by its thread ID.
如果目标线程没有监听 workerMessage 事件,则该操作会抛出 ERR_WORKER_MESSAGING_FAILED 错误。
🌐 If the target thread has no listener for the workerMessage event, then the operation will throw
a ERR_WORKER_MESSAGING_FAILED error.
如果目标线程在处理 workerMessage 事件时抛出错误,那么该操作将抛出 ERR_WORKER_MESSAGING_ERRORED 错误。
🌐 If the target thread threw an error while processing the workerMessage event, then the operation will throw
a ERR_WORKER_MESSAGING_ERRORED error.
当目标线程不是当前线程的直接父线程或子线程时,应使用此方法。如果两个线程是父子关系,请使用 require('node:worker_threads').parentPort.postMessage() 和 worker.postMessage() 来让线程之间进行通信。
🌐 This method should be used when the target thread is not the direct
parent or child of the current thread.
If the two threads are parent-children, use the require('node:worker_threads').parentPort.postMessage()
and the worker.postMessage() to let the threads communicate.
下面的例子展示了 postMessageToThread 的用法:它创建了 10 个嵌套线程,最后一个线程将尝试与主线程通信。
🌐 The example below shows the use of of postMessageToThread: it creates 10 nested threads,
the last one will try to communicate with the main thread.
import process from 'node:process';
import {
postMessageToThread,
threadId,
workerData,
Worker,
} from 'node:worker_threads';
const channel = new BroadcastChannel('sync');
const level = workerData?.level ?? 0;
if (level < 10) {
const worker = new Worker(new URL(import.meta.url), {
workerData: { level: level + 1 },
});
}
if (level === 0) {
process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
postMessageToThread(source, { message: 'pong' });
});
} else if (level === 10) {
process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
channel.postMessage('done');
channel.close();
});
await postMessageToThread(0, { message: 'ping' });
}
channel.onmessage = channel.close;'use strict';
const process = require('node:process');
const {
postMessageToThread,
threadId,
workerData,
Worker,
} = require('node:worker_threads');
const channel = new BroadcastChannel('sync');
const level = workerData?.level ?? 0;
if (level < 10) {
const worker = new Worker(__filename, {
workerData: { level: level + 1 },
});
}
if (level === 0) {
process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
postMessageToThread(source, { message: 'pong' });
});
} else if (level === 10) {
process.on('workerMessage', (value, source) => {
console.log(`${source} -> ${threadId}:`, value);
channel.postMessage('done');
channel.close();
});
postMessageToThread(0, { message: 'ping' });
}
channel.onmessage = channel.close;