Git Product home page Git Product logo

Comments (7)

josdejong avatar josdejong commented on July 18, 2024

If this is a question about TypeORM please ask at the project itself. You can ask questions here about workerpool.

from workerpool.

ghostlexly avatar ghostlexly commented on July 18, 2024

It's about workerpool, i want to find a way to use Async methods inside workerpool, like typeorm's async methods, it's just an example

from workerpool.

josdejong avatar josdejong commented on July 18, 2024

You can "just" use async functions and return Promise like results.

Here is an example: https://jsbin.com/semafew/edit?html,console

from workerpool.

ghostlexly avatar ghostlexly commented on July 18, 2024

Here is the problem i have with the same code:

import workerpool from "workerpool";
import { User } from "./entities/User";
export const queues = workerpool.pool({
  maxWorkers: require("os").cpus().length,
});

  async function getUsers() {
    function getResults() {
      return new Promise(async (resolve, reject) => {
        resolve(await User.find());
      });
    }

    return await getResults();
  }

  const test = await queues.exec(getUsers, []);

User is the entity from TypeORM.

crosslikes-backend-1  | ReferenceError: User_1 is not defined
crosslikes-backend-1  |     at eval (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:6:25)
crosslikes-backend-1  |     at new Promise (<anonymous>)
crosslikes-backend-1  |     at getResults (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:5:20)
crosslikes-backend-1  |     at getUsers (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:9:22)
crosslikes-backend-1  |     at Function.eval (eval at run (/usr/src/app/node_modules/workerpool/src/worker.js:105:11), <anonymous>:10:8)
crosslikes-backend-1  |     at Function.run (/usr/src/app/node_modules/workerpool/src/worker.js:106:12)
crosslikes-backend-1  |     at MessagePort.<anonymous> (/usr/src/app/node_modules/workerpool/src/worker.js:157:27)
crosslikes-backend-1  |     at [nodejs.internal.kHybridDispatch] (node:internal/event_target:737:20)
crosslikes-backend-1  |     at exports.emitMessage (node:internal/per_context/messageport:23:28)

from workerpool.

josdejong avatar josdejong commented on July 18, 2024

The function getUsers references external code User. The function is serialized, send to the worker, and then deserialized, and therefore must be completely standalone. In this case, on the worker side there is no User. You'll have to either embed this class in the function, or create a dedicated worker.

from workerpool.

ghostlexly avatar ghostlexly commented on July 18, 2024

Is there a way to call the dedicated worker with a class name ?
i try to avoid file paths like this

__dirname + '/myWorker.js'

Thanks for the solutions

from workerpool.

josdejong avatar josdejong commented on July 18, 2024

No, just like a regular import you need to point to the path where the worker script can be found.

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.