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

Workers to main thread communication #212

Closed
Elyahou opened this issue Mar 25, 2022 · 2 comments · Fixed by #213
Closed

Workers to main thread communication #212

Elyahou opened this issue Mar 25, 2022 · 2 comments · Fixed by #213

Comments

@Elyahou
Copy link
Contributor

Elyahou commented Mar 25, 2022

I need to add a way to send messages from workers to main thread:

The only possibility I see right now with Piscina, is to send a messagePort when running a task and use it to communicate (Or use a broadcastChannel but only for node 17+).

In my case, I want to use a single messagePort per worker, set at startup of the worker and use it when initializing inside the worker and before running tasks... But there is no currently easy way to add a messagePort per worker at startup built in Piscina...

I wonder if it would be possible to use the already existing built in messages ports of Piscina to add this feature ?

Something like this:

Adding a new kind of message that the worker can send:

export interface GenericMessage {
  data: any;
}

Use it to send messages from worker at any stage:

let message : GenericMessage;
parentPort.postMessage(message);

And handle such messages at pool level to expose it:

worker.on('message', (message : GenericMessage) => {
  publicInterface.emit('message', message.data);
}

Do this make sense ?

@jasnell
Copy link
Collaborator

jasnell commented Mar 25, 2022

Certainly a possibility. It can get a bit tricky because of timing. The messages you are sending are not guaranteed to arrive and be processed (because the worker could be torn down first) and it's likely not a good idea for those messages to be related to any tasks (or to have individual tasks depend on them) or else you can run into nasty race conditions and inconsistencies.

PRs are welcome!

@Elyahou
Copy link
Contributor Author

Elyahou commented Mar 27, 2022

@jasnell Here is a PR #213

What do you think ?

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

Successfully merging a pull request may close this issue.

2 participants