Git Product home page Git Product logo

Comments (4)

NickKelly1 avatar NickKelly1 commented on August 16, 2024 2

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.

josdejong avatar josdejong commented on August 16, 2024

How to reproduce this issue? Can you share a minimal example that demonstrates the issue?

from workerpool.

josdejong avatar josdejong commented on August 16, 2024

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.

luluhoc avatar luluhoc commented on August 16, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.