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

ParentPort with multiple workers #98

Closed
pc3b3r opened this issue Nov 26, 2020 · 3 comments
Closed

ParentPort with multiple workers #98

pc3b3r opened this issue Nov 26, 2020 · 3 comments

Comments

@pc3b3r
Copy link

pc3b3r commented Nov 26, 2020

Is there a way to have a single MessageChannel in the main process with multiple tasks like the parentPort

I tried with

(async function () {
  const channel = new MessageChannel()
  channel.port1.on('message', (message) => {
    console.log(message)
  })
  let a = 0
  while (a < 10) {
    piscina.runTask({ hello: `world${a}`, port: channel.port2 }, [channel.port2])
    a++
  }
})()

but it gets DataCloneError: MessagePort in transfer list is already detached

@jasnell
Copy link
Collaborator

jasnell commented Nov 26, 2020

MessageChannel is one-to-one only and an associated MessagePort can only be shared with a single thread. As luck would have it, yesterday I started working on an implementation of BroadcastChannel for Node.s: nodejs/node#36271

@jasnell
Copy link
Collaborator

jasnell commented Dec 1, 2020

The PR is getting closer to landing. It's a semver-minor that should be fairly easy to port back to 14.x and 12.x. The way it would work with Piscina is pretty straightforward... your main event loop and your workers would each create their own instances of new BroadcastChannel('whatever') where 'whatever' is some arbitrary shared name. Just keep in mind that any BroadcastChannel instance in the entire process could attach to the same channel and also receive the messages, which could be a good thing or bad depending on what you're doing with it.

@pc3b3r
Copy link
Author

pc3b3r commented Dec 1, 2020

@jasnell thank you for your effort. I look forward to try it asap.

@jasnell jasnell closed this as completed Dec 2, 2020
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

2 participants