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

Add completed method in Pool #354

Open
10n37 opened this issue Sep 8, 2022 · 5 comments
Open

Add completed method in Pool #354

10n37 opened this issue Sep 8, 2022 · 5 comments

Comments

@10n37
Copy link

10n37 commented Sep 8, 2022

Instead of doing every time this:

const pool = workerpool.pool(
    path.resolve(__dirname, 'worker')
)
const tasks: workerpool.Promise<any, Error>[] = []

for (const entity of entities) {
 const task = pool.exec('worker', [entity])
 tasks.push(task)
}

await Promise.all(tasks)
await pool.terminate()

We can do:

const pool = workerpool.pool(
    path.resolve(__dirname, 'worker')
)

for (const entity of entities) {
 pool.exec('worker', [entity])
}

/* Wait all tasks to complete */ 
await pool.completed()
await pool.terminate()

It would be very handy, if this method is added in the future
For example, threads.js already have this feature

@josdejong
Copy link
Owner

Thanks for your suggestion. Actually, what you want is already there: just calling await pool.terminate() will by default gracefully shutdown the workerpool. It finishes all running tasks and does not accept new tasks.

From the docs:

Pool.terminate([force: boolean [, timeout: number]])

If parameter force is false (default), workers will finish the tasks they are working on before terminating themselves. Any pending tasks will be rejected with an error 'Pool terminated'. When force is true, all workers are terminated immediately without finishing running tasks. If timeout is provided, worker will be forced to terminate when the timeout expires and the worker has not finished.

Docs: https://github.com/josdejong/workerpool#api

@10n37 10n37 closed this as completed Sep 8, 2022
@10n37
Copy link
Author

10n37 commented Sep 8, 2022

Thanks for your suggestion. Actually, what you want is already there: just calling await pool.terminate() will by default gracefully shutdown the workerpool. It finishes all running tasks and does not accept new tasks.

From the docs:

Pool.terminate([force: boolean [, timeout: number]])
If parameter force is false (default), workers will finish the tasks they are working on before terminating themselves. Any pending tasks will be rejected with an error 'Pool terminated'. When force is true, all workers are terminated immediately without finishing running tasks. If timeout is provided, worker will be forced to terminate when the timeout expires and the worker has not finished.

Docs: https://github.com/josdejong/workerpool#api

Wow, i tried. But it works not as expected. In this code pool terminated first. Tasks not executing.

const pool = workerpool.pool(
    path.resolve(__dirname, 'worker')
)

for (const entity of entities) {
 pool.exec('worker', [entity])
}

await pool.terminate()

@josdejong
Copy link
Owner

Ooo wait a second, of course: terminate only gracefully finishes all running tasks, but it will cancel and not execute queued tasks. Sorry, my bad.

So, yes, your suggestion makes sense, a method like await pool.completed(). Anyone interested in thinking this through and implementing such a method?

@Darshan-upadhyay1110
Copy link

Darshan-upadhyay1110 commented Sep 26, 2022

await pool.completed().then(result=>{ await pool.terminate(); })
is it the way we can do this ?

@josdejong
Copy link
Owner

Yes, you can keep track on all tasks and wait till they are all resolved, like the first example in #354 (comment). It's a bit cumbersome but not a showstopper.

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

No branches or pull requests

3 participants