Comments (4)
Had the same issue in NodeJS using worker threads.
Solution was to stop calling workerpool.worker(...)
in the main thread. workerpool.worker
tries to communicate with the main thread which breaks when executed from the main thread. It tries to use parentPort
but that only exists on worker threads so the package falls back to process.send
which doesn't exist either because the process wasn't spawned with an IPC channel, hence the error message.
For example:
package.json
{
"type": "module",
"dependencies": {
"workerpool": "^6.4.0"
}
}
main.js
// cjs
// const wt = require('worker_threads');
// const wp = require('workerpool');
// esm
import { fileURLToPath } from 'url';
import * as wt from 'worker_threads';
import wp from 'workerpool';
const ctx = wt.isMainThread ? '[main]' : '[worker]';
console.log(ctx);
// if (!wt.isMainThread) {
wp.worker({
work() { console.log(ctx, 'working'); }
});
// }
if (wt.isMainThread) {
// const thisFilename = __filename; // cjs
const thisFilename = fileURLToPath(import.meta.url); // esm
const pool = wp.pool(thisFilename, { workerType: 'thread', });
pool
.exec('work', null, { })
.then(function() { console.log(ctx, 'done'); })
.catch(function(err) { console.log(ctx, 'error', err); })
.then(function() { pool.terminate(); })
}
execution
➜ example node main.js
[main]
.../example/node_modules/workerpool/src/worker.js:63
process.send(message);
^
TypeError: process.send is not a function
at worker.send (.../example/node_modules/workerpool/src/worker.js:63:15)
at worker.register [as add] (.../example/node_modules/workerpool/src/worker.js:238:10)
at Object.worker (.../example/node_modules/workerpool/src/index.js:22:10)
at file:///.../example/main.js:13:6
at ModuleJob.run (node:internal/modules/esm/module_job:192:25)
Node.js v20.2.0
Fixed by uncommenting the conditional // if (!wt.isMainThread) {
from workerpool.
How to reproduce this issue? Can you share a minimal example that demonstrates the issue?
from workerpool.
Thanks for sharing Nick!
The workerpool.worker
method is indeed only usable on the worker side, not in the main thread.
I'll close this issue now.
from workerpool.
Just for anybody having this problem, for me it was that I accidentally exported a function and imported it in main thread 🔢
from workerpool.
Related Issues (20)
- child_process.spawn() or child_process.exec() HOT 1
- (question) How to override the .exec function ? HOT 2
- How to use TypeORM or Prisma inside worker ? HOT 7
- how to use FileReaderSync in workerpool HOT 2
- How can a worker listen to master's specific event? HOT 1
- Question: complex objects to worker HOT 3
- Not executing function that depends on variable/function out of scope. HOT 1
- : Cannot find name 'Transferable'. Did you mean 'Transfer'? HOT 2
- MongooDB instance is not connecting in dedicated worker HOT 10
- [benchmark] benchmark for various thread pools, but I am not sure if it is rigorous enough HOT 1
- [help wanted] Additional functions in worker context HOT 2
- Workerpool Error when using webpack HOT 3
- Make sure stats are always consistent when handling a task result HOT 5
- terminate(true) didn't kill the pool process HOT 3
- [fork-join] Can / How assign task to workers HOT 7
- capture stdio during execution of a task HOT 2
- Guidance on Restarting a Specific Service in Workerpool to Handle Memory Leaks in Playwright HOT 3
- `worker.exit` should set `process.exitCode` instead of calling `process.exit` to allow cleanup HOT 4
- What should I do to solve the error I introduced into the vue project? Do I need to configure something in webpack HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from workerpool.