Where communities thrive


  • Join over 1.5M+ people
  • Join over 100K+ communities
  • Free without limits
  • Create your own community
People
Activity
    Andy Wermke
    @andywer
    @rubiin the pasted code's formatting is broken and thus really hard to read. but it looks like you are already creating the pool on app start.
    Rubin Bhandari
    @rubiin
    i am using something like this:
    const isCorrect = captchaPool.queue((auth) => auth.verify(captcha, captcha_token)) console.info('auth is ',isCorrect)
    i should get a boolean value but getting auth is { id: 1, run: [Function], cancel: [Function: cancel], then: [Function: bound then] }
    Rubin Bhandari
    @rubiin
    const hashed = captchaPool.queue((auth) => auth.hash()) this is working though , its weird
    Andy Wermke
    @andywer
    @rubiin the last snippet shouldn't work either, but what happens is that pool.queue() returns the queued job. you can await pool.queue() to obtain the job's result. be aware, though, that if you await the result directly on queueing, you will only queue another job after this one has finished, so you might rather want to .then() the old-fashioned way: pool.queue(…).then(result => console.log('auth is', result)).
    Rubin Bhandari
    @rubiin
    @andywer i think the documentation can use a little update on the thread pool section on how to get results back
    Andy Wermke
    @andywer
    Fair enough! If you have some concrete suggestions, a PR proposing a documentation update would be much appreciated 🙂
    Rubin Bhandari
    @rubiin
    i will see what i can do. Will be good for me to take reference in future too

    @rubiin the last snippet shouldn't work either, but what happens is that pool.queue() returns the queued job. you can await pool.queue() to obtain the job's result. be aware, though, that if you await the result directly on queueing, you will only queue another job after this one has finished, so you might rather want to .then() the old-fashioned way: pool.queue(…).then(result => console.log('auth is', result)).

    that solves it.

    my function was returning a boolean promise so const isCorrect = captchaPool .queue(auth => auth.verify(captcha, captcha_token)) .then(result => { return result; }); did the trick
    Rubin Bhandari
    @rubiin
    i will open a pr in free time about this
    Andy Wermke
    @andywer
    sounds great 👍
    Rubin Bhandari
    @rubiin
    opened a pr
    the docs could also use a faq page , more convinient than answering same question on github
    Rubin Bhandari
    @rubiin
    also is there a way to define the worker function in a class

    import { expose } from 'threads/worker';

    import { argon2d, hash } from 'argon2';

    const password = {
    hashString(value: string): Promise<string> {
    return hash(value, {
    type: argon2d,
    hashLength: 50,
    saltLength: 32,
    timeCost: 4,
    });
    },
    };

    export type Password = typeof password;

    expose(password);
    `

    i wanted to define this in a js class if possible
    Andy Wermke
    @andywer
    I guess you can expose(new MyThing())
    Rubin Bhandari
    @rubiin
    i think the doc also needs an update on error handling incase of thread pool. if you do like thread, you get uncaught promiseexception
    return captchaPool .queue(async auth => await auth.verify(captcha, captcha_token)) .then(result => { return result; }) .catch(e => { throw new InternalServerErrorException(); }) .finally(async () => { await captchaPool.completed(); });
    this works flawlessly
    Rubin Bhandari
    @rubiin
    @andywer Uses web workers in the browser, worker_threads in node 12+ and tiny-worker in node 8 to 11. worker_threads is it still applied to 12?. The description seems misleading
    Andy Wermke
    @andywer
    @rubiin .catch(e => { throw new InternalServerErrorException(); }) - what did you expect? you are replacing one error with another one and then don't handle that new error 😉

    worker_threads in node 12+

    worker_threads is it still applied to 12?. The description seems misleading

    i have no idea what you're trying to say, tbh. what's unclear about threads.js using worker_threads in node 12 and later?

    Rubin Bhandari
    @rubiin

    @rubiin .catch(e => { throw new InternalServerErrorException(); }) - what did you expect? you are replacing one error with another one and then don't handle that new error 😉

    without the catch statement , i was getting unhandled promise rejection even if i wrap the whole thing on try catch

    node version 12 uses worker_threads or tiny-worker? that statement is confusing as it has 12+ for worker_threads
    Andy Wermke
    @andywer
    12+ means “12 and above”
    Rubin Bhandari
    @rubiin
    can we reuse a pool or do we need to have different pool for different workers
    Andy Wermke
    @andywer
    i think there is no way to re-create the workers of an existing pool, so you will have to create a new one. i don't think you would really gain anything from re-using a pool either.
    Rubin Bhandari
    @rubiin
    reusable thread pool
    Andy Wermke
    @andywer
    there was a PR, but i don't think it's been merged.
    but again: what would be the benefit of that? the pool itself doesn't do much, it's the workers that are resource hungry and take quite some time to initialize. re-purposing a pool doesn't seem to make much sense if you want to replace its workers as initializing the workers is 99% of the work.
    Rubin Bhandari
    @rubiin
    i mean like have a pool of workers on standby then dynamically give them jobs
    Andy Wermke
    @andywer
    you can do that already. just create a pool and only call it once you’ve got work to do 😉
    Rubin Bhandari
    @rubiin
    also any update on incoming features
    Andy Wermke
    @andywer
    not too much upcoming right now. plenty busy with other things atm tbh.
    Konrad Lewandowski
    @korrix
    Hello. I am having trouble compiling my typescript library after adding the threadjs dependency:
    node_modules/threads/node_modules/observable-fns/dist/observable.d.ts:57:10 - error TS2368: Type parameter name cannot be 'never'.
    
    57     pipe<never, Out extends ObservableLike<T>>(...mappers: Array<(input: Out) => Out>): Out;
                ~~~~~
    @andywer Maybe you will have some clue why such error happens...
    Andy Wermke
    @andywer
    @korrix Yes, a PR with a fix is already open. I can merge and release in the next few hours.
    See #374
    Andy Wermke
    @andywer
    Published as v1.6.5
    Yuval Marcus
    @ymarcus93
    Does anyone have experience in using threads.js with a custom webpack in Angular? I'm using @angular-builders/custom-webpack and threads-plugin, but I still get a 404 error when the worker thread is spawned and tries to look for my worker file. This sounds like the bundler is not working correctly, so I must have mis-configured it but I'm not sure.
    Yuval Marcus
    @ymarcus93
    Is there a way to use threads.js without having to use custom webpack?
    Andy Wermke
    @andywer
    No, you need to make your bundler create a separate bundle for the worker – that’s what the threads-plugin is for.
    PS: webpack 5 is not supported out of the box yet. sorry for that, but it’s trickier than expected and i don’t have much time these days. hope we can fix that soon, so people can finally ditch webpack 4.
    BenHeartfield
    @BenHeartfield
    Hi, I've implemented a workaround for Webpack 5 - see https://gist.github.com/BenHeartfield/701cc91de54ca3a0da59a5a16dfb2e44
    it uses raw regex search to find&replace new Worker('my-worker.js') with new Worker('my-worker.hash.js') with the appropriate hash. An AST-based solution would be preferable, but this should do for now - enjoy. An issue was opened asking to document this workaround on the site & both repos (the plugin & threads.js itself) andywer/threads-plugin#45 .
    BenHeartfield
    @BenHeartfield
    Is there a plan to introduce promise-based locks/semaphores/conditions to this library? Something like C#'s SemaphoreSlim#WaitAsync? I've seen single-threaded async locks, such as this guy's - https://blog.jcoglan.com/2016/07/12/mutexes-and-javascript/ - but would like to see this extended to a multi-threaded env.
    Andy Wermke
    @andywer
    @BenHeartfield Awesome! Thanks for sharing! Will try to come up with an update for the threads-plugin in the next few days.
    Didn’t really consider semaphores/mutexes yet, tbh. What’s your use case? 😉
    BenHeartfield
    @BenHeartfield
    With pleasure :)
    No specific use case in mind, it just seems like a healthy thing to have in a language. Without it concurrent edits to SharedArrayBuffer/localStorage become problematic, in the same vein as the mutexes-and-javascript link shows.