andywer / threads.js

🧵 Make web workers & worker threads as simple as a function call.
https://threads.js.org/
MIT License
3.08k stars 163 forks source link

program just quits when spawning worker #239

Closed MichaelLiss closed 4 years ago

MichaelLiss commented 4 years ago

Hi

I am using Visual Code on a Windows box.

node version: 12.16.0 npm version: 6.14.4

package dependencies:

    "axios": "^0.19.2",
    "bcryptjs": "^2.4.3",
    "bull": "^3.7.0",
    "concurrently": "^5.2.0",
    "cors": "^2.8.5",
    "express": "^4.17.1",
    "fit-file-parser": "^1.6.18",
    "heroku-ssl-redirect": "0.0.4",
    "jsonwebtoken": "^8.5.1",
    "mongoose": "^5.9.10",
    "nodemailer": "^6.4.6",
    "oauth-signature": "^1.5.0",
    "randomstring": "^1.1.5",
    "threads": "^1.4.1",
    "throng": "^4.0.0",
    "validator": "^12.2.0",
    "xml2js": "^0.4.23"

When I start the debugger and step through the code, my app just disappears on this line: (it doesn't hit the catch either)

const auth = await spawn(new Worker(workerScript));

Here is the entire code:

startWorker_Test = async () => {
    try{
        startWorker_Threads_Password();
    }
    catch(err){
        console.log(err);
    }
}

startWorker_Threads_Password = async () => {
    console.log('startWorker_Threads_Password - this function will call auth.js and hash a password');

    let workerScript = path.join(__dirname, '/auth.js');
    workerScript = './auth.js';
    console.log('startWorker_Threads_Password - spawning worker', workerScript);

    const auth = await spawn(new Worker(workerScript));

    console.log('startWorker_Threads_Password - hasing password');
    const hashed = await auth.hashPassword("Super secret password", "1234");

    console.log("Hashed password:", hashed)

    console.log('startWorker_Threads_Password - terminating thread');
    await Thread.terminate(auth)    
}

Auth.js contains the following:

// workers/auth.js
//import sha256 from "js-sha256"
import { expose } from "threads/worker"

expose({
  hashPassword(password, salt) {
    return 'test';
  }
})
MichaelLiss commented 4 years ago

Auth.js is in the same directory as the file that spawns the worker but

full path fails:

let workerScript = path.join(__dirname, '/auth.js');

(node:13840) UnhandledPromiseRejectionWarning: Error: Cannot find module 'c:\Users\mliss\Documents\GitHub\instanttiming\routes\api\c:\Users\mliss\Documents\GitHub\instanttiming\routes\api\auth.js'

and relative path:

workerScript = './auth.js';
// or
workerScript = './auth';

gives this error and then just disappears

SyntaxError
andywer commented 4 years ago

Hey @MichaelLiss!

Here is the entire code

Does not look like the entire code. Where do you import from threads?

My best guess is that you use require() in the master thread code and have the ES imports in the worker code that node.js chokes upon.

Btw, are you transpiling the code in any way – Babel, webpack, …?

MichaelLiss commented 4 years ago

Thanks for replying...

installed it like this : (as my node version is 12.16.0 )

npm install threads

here is the stuff I have modified in order to just get it to the point it will fail:

if I do this at the top of my file:

import { spawn, Thread, Worker } from "threads";

I get this:

Waiting for thed:\Code\Udemy\instanttiming\routes\api\garminFunctions.js:1
import { spawn, Thread, Worker } from "threads";
^^^^^^

SyntaxError: Cannot use import statement outside a module

So I changed it to:

const { spawn, Thread, Worker } = require("threads");

I changed my code to be JUST like the Quick start code (except the auth.js file is in the same directory as the file that calls it )

so my function looks like this:

startWorker_Threads = async () => {

  const auth = await spawn(new Worker("./auth"))
  const hashed = await auth.hashPassword("Super secret password", "1234")

  console.log("Hashed password:", hashed)

  await Thread.terminate(auth);
}
MichaelLiss commented 4 years ago

ok.. got it... you were correct changed auth.js to

// workers/auth.js - will be run in worker thread
//import sha256 from "js-sha256"
const { expose } = require("threads/worker");

expose({
  hashPassword(password, salt) {
    return 'faking it';
  }
})