worker_threads#parentPort TypeScript Examples
The following examples show how to use
worker_threads#parentPort.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: saved-model.worker-thread.ts From node-question-answering with Apache License 2.0 | 6 votes |
parentPort?.on("message", (value: Message) => {
switch (value.type) {
case "infer":
runInference(value);
break;
case "init":
loadPort = value.loadPort;
inferencePort = value.inferencePort;
value.initPort.close();
break;
case "load":
initModel(value.params);
break;
}
});
Example #2
Source File: worker-client.ts From uivonim with GNU Affero General Public License v3.0 | 6 votes |
parentPort!.on('message', async ([e, data, id]) => {
if (e === '@@sab') {
return
}
if (!id) return ee.emit(e, ...data)
if (pendingRequests.has(id)) {
pendingRequests.get(id)(data)
pendingRequests.delete(id)
return
}
const listener = ee.listeners(e)[0]
if (!listener) return
const result = await listener(...data)
send([e, result, id])
})
Example #3
Source File: wrapWorkerFunc.ts From joplin-utils with MIT License | 6 votes |
/**
* 包装需要放到 worker 中执行的函数
* 1. 当检查到当前文件不是 js 文件时会直接返回函数
* 2. 当检查到在主线程时执行时,使用 Worker 包装并执行它
* 3. 当检查到在 Worker 线程时,使用 expose 包装它然后执行
* 注:目前是每次都创建新的 Worker,也许可以考虑支持复用 Worker
* @param ep
*/
export function wrapWorkerFunc<T extends (...args: any[]) => any>(
ep: T,
): Remote<T> {
// return ep as Remote<T>
if (path.extname(__filename) !== '.js') {
return ep as Remote<T>
}
if (isMainThread) {
return ((...args: any[]) => {
const worker = new Worker(__filename)
const fn = wrap<T>(nodeEndpoint(worker))
return (fn(...args) as Promise<any>).finally(() => worker.unref())
}) as Remote<T>
}
expose(ep, nodeEndpoint(parentPort!))
return ep as Remote<T>
}
Example #4
Source File: deserializer.ts From eosio-contract-api with GNU Affero General Public License v3.0 | 6 votes |
parentPort.on('message', (param: Array<{type: string, data: Uint8Array | string, abi?: any}>) => {
try {
const result = [];
for (const row of param) {
if (row.data === null) {
return parentPort.postMessage({success: false, message: 'Empty data received on deserialize worker'});
}
if (row.abi) {
const abiTypes = Serialize.getTypesFromAbi(Serialize.createInitialTypes(), row.abi);
result.push(deserializeEosioType(row.type, row.data, abiTypes));
} else {
result.push(deserializeEosioType(row.type, row.data, eosjsTypes));
}
}
return parentPort.postMessage({success: true, data: result});
} catch (e) {
return parentPort.postMessage({success: false, message: String(e)});
}
});
Example #5
Source File: auth-redirect-server.ts From relate with GNU General Public License v3.0 | 6 votes |
requestListener = (req: http.IncomingMessage, res: http.ServerResponse): void => {
const {authToken} = url.parse(req.url || '', true).query;
if (!authToken) {
throw new AuthenticationError('Failed to authenticate');
}
if (parentPort && parentPort.postMessage) {
parentPort.postMessage(authToken);
}
res.writeHead(200);
res.write(
'<script type="text/javascript">window.close()</script>You are authenticated, you can close this tab now.',
() => {
res.end(() => process.exit());
},
);
}
Example #6
Source File: youtube.worker.ts From Discord-SimpleMusicBot with GNU General Public License v3.0 | 6 votes |
// DO NOT import unnecessary module preventing from infinite spawned workers.
parentPort.on("message", (value) => {
const data = value as workerMessage;
if(data && data.type === "init"){
const { id, url, prefetched, forceCache } = data;
const youtube = new YouTube();
youtube.init(url, prefetched, forceCache).then(() => {
parentPort.postMessage({
type: "initOk",
data: youtube,
id,
} as workerInitSuccessMessage & {type:number});
}).catch((er) => {
parentPort.postMessage({
type: "error",
data: er,
id,
} as workerErrorMessage & {type:number});
});
}else if(data && data.type === "search"){
const id = data.id;
ytsr.default(data.keyword, {
limit:12,
gl: "JP",
hl: "ja"
}).then((result) => {
parentPort.postMessage({
type: "searchOk",
data: result,
id
} as workerSearchSuccessMessage & {type:number})
}).catch((er) => {
parentPort.postMessage({
type: "error",
data: er,
id
} as workerErrorMessage & {type:number});
});
}
})
Example #7
Source File: test-result-emitter-worker.ts From karma-test-explorer with MIT License | 6 votes |
initWorker = () => {
if (!parentPort || !workerData) {
return;
}
const messagePort = parentPort;
const { socketPort, pingTimeout, pingInterval }: TestResultEmitterWorkerData = workerData;
const socket = io('http://localhost:' + socketPort + '/', {
forceNew: true,
reconnection: true
});
const socketOptions = { pingTimeout, pingInterval };
Object.assign(socket, socketOptions);
messagePort.on('message', (event: KarmaEvent) => {
socket.emit(event.name, event);
});
}
Example #8
Source File: staging_clean.ts From Deep-Lynx with MIT License | 6 votes |
void postgresAdapter
.init()
.then(() => {
DataStagingMapper.Instance.DeleteOlderThanRetention()
.then(() => {
DataStagingMapper.Instance.Vacuum()
.then(() => {
void PostgresAdapter.Instance.close()
if (parentPort) parentPort.postMessage('done');
else {
process.exit(0);
}
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unable to run staging cleaning job ${e}`);
process.exit(1);
});
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unable to run staging cleaning job ${e}`);
process.exit(1);
});
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in staging clean thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #9
Source File: orphan_edge_linker.ts From Deep-Lynx with MIT License | 6 votes |
void postgresAdapter
.init()
.then(() => {
EdgeMapper.Instance.RunEdgeLinker()
.catch((e) => {
Logger.error(`unable to run edge linker job ${e}`);
})
.finally(() => {
void PostgresAdapter.Instance.close()
if (parentPort) parentPort.postMessage('done');
else {
process.exit(0);
}
})
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in orphan edge linker thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
})
Example #10
Source File: monitor.ts From Cromwell with MIT License | 6 votes |
parentPort?.on('message', async (message) => {
if (message?.command === 'getSysInfo') {
const info = await getSysInfo();
parentPort?.postMessage({
type: 'sysInfo',
info,
});
}
if (message?.command === 'getUsageInfo') {
const info = await getUsageInfo();
parentPort?.postMessage({
type: 'usageInfo',
info,
});
}
})
Example #11
Source File: worker.ts From omegga with ISC License | 6 votes |
emit = (action: string, ...args: any[]) => {
const messageId = 'message:' + messageCounter++;
let rejectFn: (reason?: any) => void;
// promise waits for the message to resolve
const promise = new Promise((resolve, reject) => {
parent.once(messageId, resolve);
rejectFn = reject;
});
try {
// post the message
parentPort.postMessage({ action, args: [messageId, ...args] });
} catch (err) {
rejectFn(err);
}
// return the promise
return promise;
}
Example #12
Source File: indexer_worker.ts From aurora-relayer with Creative Commons Zero v1.0 Universal | 6 votes |
async function main(parentPort: MessagePort, workerData: WorkerData) {
const { config, network, env } = workerData;
const engine = await Engine.connect(
{
network: network.id,
endpoint: config.endpoint,
contract: config.engine,
},
env
);
const indexer = new Indexer(config, network, engine);
await indexer.start();
setTimeout(function () {
indexer.insertNewHeads();
});
parentPort
.on('message', async (block: any) => {
parentPort.postMessage(true); // ack the request
const blockData = await indexer.indexBlock(block.id);
if (block.is_head) {
indexer.notifyNewHeads(block.id, blockData);
} else {
indexer.insert(blockData);
}
})
.on('close', () => {
return; // TODO?
});
}
Example #13
Source File: create-data-service-worker.ts From amplication with Apache License 2.0 | 6 votes |
parentPort?.once("message", (params: WorkerParam) => {
const { entities, roles, appInfo } = params;
const loggerCallback = (message: string) => {
const messageData: WorkerResult = {
message,
done: false,
};
parentPort?.postMessage(messageData);
};
const logger = createWorkerLogger(loggerCallback);
createDataServiceImpl(entities, roles, appInfo, logger)
.then((modules) => {
const results: WorkerResult = {
done: true,
modules,
};
parentPort?.postMessage(results);
parentPort?.close();
})
.catch((error) => {
const results: WorkerResult = {
error: error,
done: false,
};
parentPort?.postMessage(results);
parentPort?.close();
});
});
Example #14
Source File: healthWorker.ts From h1z1-server with GNU General Public License v3.0 | 6 votes |
function checkHealth() {
const { threadToWatchPid } = workerData;
let healthTimeoutTimer: any;
const healthTimer = setTimeout(() => {
parentPort?.postMessage(true);
healthTimeoutTimer = setTimeout(() => {
process.kill(threadToWatchPid);
}, 25000);
}, 10000);
parentPort?.on("message", () => {
healthTimer.refresh();
clearTimeout(healthTimeoutTimer);
});
}
Example #15
Source File: soeClient.ts From h1z1-server with GNU General Public License v3.0 | 6 votes |
parentPort.on("message", (action) => {
switch (action) {
case "connect":
console.log("goooo");
soeClient.connect();
break;
default:
break;
}
});
Example #16
Source File: soeServer.ts From h1z1-server with GNU General Public License v3.0 | 6 votes |
async function setup() {
new SOEServer(
"test",
1115,
Buffer.from("F70IaxuU8C/w7FPXY1ibXw==", "base64"),
0,
false
).start(0, 0, 2, 512);
parentPort.postMessage("started");
}
Example #17
Source File: httpServer.ts From h1z1-server with GNU General Public License v3.0 | 6 votes |
parentPort?.on(`message`, (message: httpServerMessage) => {
const { type, requestId, data } = message;
switch (type) {
case "pingzone": {
const res = pendingRequest[requestId];
if (data === "pong") {
res.writeHead(200, { "Content-Type": "text/json" });
res.write(data);
} else {
res.writeHead(404, { "Content-Type": "text/json" });
}
res.end();
delete pendingRequest[requestId];
break;
}
case "ping":
const res = pendingRequest[requestId];
res.writeHead(200, { "Content-Type": "text/json" });
res.write(data);
res.end();
delete pendingRequest[requestId];
break;
default:
break;
}
});
Example #18
Source File: indexer_worker.ts From aurora-relayer with Creative Commons Zero v1.0 Universal | 5 votes |
main(parentPort!, workerData as WorkerData);
Example #19
Source File: loginServer.ts From h1z1-server with GNU General Public License v3.0 | 5 votes |
async function setup() {
await new LoginServer(1115).start();
parentPort.postMessage("started");
}
Example #20
Source File: worker-client.ts From uivonim with GNU Affero General Public License v3.0 | 5 votes |
send = (data: any) => parentPort!.postMessage(data)
Example #21
Source File: soeClient.ts From h1z1-server with GNU General Public License v3.0 | 5 votes |
soeClient.on("connect", () => {
parentPort.postMessage("ok");
});
Example #22
Source File: zoneServer.ts From h1z1-server with GNU General Public License v3.0 | 5 votes |
async function setup() {
await new ZoneServer(
1117,
Buffer.from("F70IaxuU8C/w7FPXY1ibXw==", "base64")
).start();
parentPort.postMessage("started");
}
Example #23
Source File: httpServer.ts From h1z1-server with GNU General Public License v3.0 | 5 votes |
function sendMessageToServer(type: string, requestId: number, data: any) {
const message: httpServerMessage = {
type: type,
requestId: requestId,
data: data,
};
parentPort?.postMessage(message);
}
Example #24
Source File: processing_queue.ts From Deep-Lynx with MIT License | 5 votes |
void PostgresAdapter.Instance.init()
.then(() => {
void QueueFactory()
.then((queue) => {
const destination = new Writable({
objectMode: true,
write(chunk: any, encoding: string, callback: (error?: Error | null) => void) {
const stagingRecord = plainToClass(DataStaging, chunk as object);
ProcessData(stagingRecord)
.then((result) => {
if (result.isError) {
Logger.error(`processing error: ${result.error?.error}`);
}
callback();
})
.catch((e) => {
Logger.error(`unable to process data from queue ${e}`);
callback();
});
return true;
},
});
destination.on('error', (e: Error) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in processing queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
queue.Consume(Config.process_queue, destination);
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in processing queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in processing queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #25
Source File: export.ts From Deep-Lynx with MIT License | 5 votes |
void postgresAdapter
.init()
.then(() => {
const exporterRepo = new ExporterRepository();
const queue = new PQueue({concurrency: Config.export_data_concurrency});
exporterRepo
.where()
.status('eq', 'processing')
.list()
.then((exporters) => {
if (exporters.isError) Logger.error(`unable to list exporters ${exporters.error?.error}`);
const tasks = [];
for (const exporter of exporters.value) {
if (exporter) {
tasks.push(queue.add(() => exporter?.Restart(SuperUser)));
}
}
Promise.all(tasks)
.catch((e) => {
Logger.error(`unable to process exports ${e}`);
})
.finally(() => {
void PostgresAdapter.Instance.close();
if (parentPort) parentPort.postMessage('done');
else {
process.exit(0);
}
})
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unable to restart exports ${e}`)
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in export thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #26
Source File: events_queue.ts From Deep-Lynx with MIT License | 5 votes |
void PostgresAdapter.Instance.init()
.then(() => {
void QueueFactory().then((queue) => {
const destination = new Writable({
objectMode: true,
write(chunk: any, encoding: string, callback: (error?: Error | null) => void) {
const event = plainToClass(Event, chunk as object);
processFunction(event)
.then(() => {
callback();
})
.catch((e) => {
Logger.error(`unable to process event from queue ${e}`);
callback();
});
},
});
destination.on('error', (e: Error) => {
void PostgresAdapter.Instance.close();
Logger.error(`unexpected error in processing queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
queue.Consume(Config.events_queue, destination);
});
})
.catch((e) => {
void PostgresAdapter.Instance.close();
Logger.error(`unexpected error in events queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #27
Source File: data_source_queue.ts From Deep-Lynx with MIT License | 5 votes |
void PostgresAdapter.Instance.init()
.then(() => {
const dataSourceRepo = new DataSourceRepository();
void QueueFactory()
.then((queue) => {
const destination = new Writable({
objectMode: true,
write(chunk: any, encoding: string, callback: (error?: Error | null) => void) {
dataSourceRepo
.findByID(chunk as string)
.then((source) => {
if (source.isError) {
Logger.error(`unable to fetch data source ${source.error?.error}`);
callback();
return;
}
source.value
.Run()
.then(() => {
callback();
})
.catch((e) => {
Logger.error(`unable to process event from queue ${e}`);
callback();
});
})
.catch((e) => {
Logger.error(`unable to process event from queue ${e}`);
callback();
});
return true;
},
});
destination.on('error', (e: Error) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in data source queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
queue.Consume(Config.data_sources_queue, destination);
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in data source queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
})
.catch((e) => {
void PostgresAdapter.Instance.close()
Logger.error(`unexpected error in data source queue thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #28
Source File: data_source_emitter.ts From Deep-Lynx with MIT License | 5 votes |
void postgresAdapter
.init()
.then(() => {
QueueFactory()
.then((queue) => {
void postgresAdapter.Pool.connect((err, client, done) => {
const stream = client.query(new QueryStream(dataSourceMapper.listAllActiveStatement()));
const putPromises: Promise<boolean>[] = [];
stream.on('data', (data) => {
// we're simply putting the id on the queue here
putPromises.push(queue.Put(Config.data_sources_queue, plainToClass(DataSourceRecord, data as object).id));
});
stream.on('error', (e: Error) => {
Logger.error(`unexpected error in data source emitter thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
stream.on('end', () => {
done();
Promise.all(putPromises)
.catch((e) => {
Logger.error(`unable to put data sources on queue ${e}`);
})
.finally(() => {
void PostgresAdapter.Instance.close();
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
})
});
stream.pipe(devnull({objectMode: true}));
});
})
.catch((e) => {
void PostgresAdapter.Instance.close();
Logger.error(`unable to initiate data source emitter: ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
})
.catch((e) => {
void PostgresAdapter.Instance.close();
Logger.error(`unexpected error in data source emitter thread ${e}`);
if (parentPort) parentPort.postMessage('done');
else {
process.exit(1);
}
});
Example #29
Source File: worker.ts From omegga with ISC License | 5 votes |
// handle message passing
parentPort.on('message', ({ action, args }) => parent.emit(action, ...args));