Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

"worker_threads" library every time create new thread #4486

Open
2 tasks done
kr-sandipprajapati opened this issue Oct 7, 2024 · 3 comments
Open
2 tasks done

"worker_threads" library every time create new thread #4486

kr-sandipprajapati opened this issue Oct 7, 2024 · 3 comments

Comments

@kr-sandipprajapati
Copy link

Node.js Version

v20.16.0

NPM Version

10.8.1

Operating System

All

Subsystem

worker_threads

Description

when I am working with threads, nodeJs doesn't have any way to kill or sleep old threads. I think it is continuously creating new threads and I did not find any example to reuse my old threads.

Minimal Reproduction

No response

Output

No response

Before You Submit

  • I have looked for issues that already exist before submitting this
  • My issue follows the guidelines in the README file, and follows the 'How to ask a good question' guide at https://stackoverflow.com/help/how-to-ask
@avivkeller
Copy link
Member

avivkeller commented Oct 8, 2024

when I am working with threads, nodeJs doesn't have any way to kill or sleep old threads. I think it is continuously creating new threads and I did not find any example to reuse my old threads.

Can you provide an example of what it is that you are looking for? IIRC worker threads use a predefined script, so what do you mean when you say "reuse"

@kr-sandipprajapati
Copy link
Author

For example, if I create a for loop and I use a new Worker function, it will create a new thread for me every time, right? but I think it will create a thread infinite times in regular terms it is okay and it worked well. but I believe there is any chance that user can add number of threads he want to create. like if I have work which will be done in 3 threads why I should go and create multiple thread every time.

@nicholaswmin
Copy link

nicholaswmin commented Oct 27, 2024

Hi, you need to implement a thread pool; what you're asking for isn't included in the box.

From the docs:

The above example spawns a Worker thread for each parseJSAsync() call. In practice, use a pool of Workers for these kinds of tasks. Otherwise, the overhead of creating Workers would likely exceed their benefit.

When implementing a worker pool, use the AsyncResource API to inform diagnostic tools (e.g. to provide asynchronous stack traces) about the correlation between tasks and their outcomes. See "Using AsyncResource for a Worker thread pool" in the async_hooks documentation for an example implementation.

It's the exact same concept as a db connection pool and in fact you can reuse a pool across both CPU-bound and I/O-bound problems with a generic pool; there's many, i.e: https://github.com/vincit/tarn.js/; or Piscina.

At a minimum it's entirely pointless to attempt any type of concurrency without it; you said it correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants