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

some code example outdated in the doc for worker_threads #21714

Closed
skyitachi opened this issue Jul 9, 2018 · 7 comments
Closed

some code example outdated in the doc for worker_threads #21714

skyitachi opened this issue Jul 9, 2018 · 7 comments

Comments

@skyitachi
Copy link

  • Version: 10.5.0
  • Platform: Darwin
  • Subsystem:

I found in the docs for the Worker following code example is outdated

const assert = require('assert');
const {
  Worker, MessageChannel, MessagePort, isMainThread
} = require('worker_threads');
if (isMainThread) {
  const worker = new Worker(__filename);
  const subChannel = new MessageChannel();
  worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);
  subChannel.port2.on('message', (value) => {
    console.log('received:', value);
  });
} else {
  require('worker_threads').once('workerMessage', (value) => {
    assert(value.hereIsYourPort instanceof MessagePort);
    value.hereIsYourPort.postMessage('the worker is sending this');
    value.hereIsYourPort.close();
  });
}

should use parentPort.on("message") replaced

@vsemozhetbyt
Copy link
Contributor

I am a bit confused. There is no .once('workerMessage' in the linked doc.

@mullim
Copy link

mullim commented Jul 9, 2018

This is brand new and experimental (link to docs). It is not outdated and was introduced in Node 10.5.0

@skyitachi
Copy link
Author

@vsemozhetbyt please checkout the url
@mulligan121 thanks, but the code

const assert = require('assert');
const {
  Worker, MessageChannel, MessagePort, isMainThread
} = require('worker_threads');
if (isMainThread) {
  const worker = new Worker(__filename);
  const subChannel = new MessageChannel();
  worker.postMessage({ hereIsYourPort: subChannel.port1 }, [subChannel.port1]);
  subChannel.port2.on('message', (value) => {
    console.log('received:', value);
  });
} else {
  require('worker_threads').once('message', (value) => {
    assert(value.hereIsYourPort instanceof MessagePort);
    value.hereIsYourPort.postMessage('the worker is sending this');
    value.hereIsYourPort.close();
  });
}

still cannot run on 10.6.0, you can make a try, the following is my running result

node --experimental-worker worker.js

events.js:167
      throw er; // Unhandled 'error' event
      ^
TypeError: require(...).once is not a function
    at Object.<anonymous> (/Users/skyitachi/tmp/node/worker.js:13:29)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:742:12)
    at MessagePort.port.on (internal/worker.js:425:27)
    at MessagePort.emit (events.js:182:13)
    at MessagePort.onmessage (internal/worker.js:66:8)
Emitted 'error' event at:
    at Worker.[kOnErrorMessage] (internal/worker.js:296:10)
    at Worker.[kOnMessage] (internal/worker.js:306:37)
    at MessagePort.Worker.(anonymous function).on (internal/worker.js:243:57)
    at MessagePort.emit (events.js:182:13)
    at MessagePort.onmessage (internal/worker.js:66:8)

@devsnek
Copy link
Member

devsnek commented Jul 10, 2018

it should be parentPort.once not require('worker_threads')

@vsemozhetbyt
Copy link
Contributor

There seems to be a fixing PR: #21486

@skyitachi
Copy link
Author

@vsemozhetbyt thanks

@x-077
Copy link

x-077 commented Apr 3, 2019

You are missing the ParentPort in the import.

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

5 participants