GithubHelp home page GithubHelp logo

rokid / node-webworker Goto Github PK

View Code? Open in Web Editor NEW
55.0 12.0 1.0 64 KB

Threads in Node.js with Web Worker

License: Apache License 2.0

Python 0.47% JavaScript 78.65% C++ 20.88%
nodejs webworker threading

node-webworker's Introduction

node-webworker

Web Worker implementation for Node.js. This module provides multi-threads programming for Node.js user-land by borrowing the concept of the standard Web Worker.

Get Started

'use strict';
const WebWorker = require('webworker-ng').WebWorker;
const worker = new WebWorker((self) => {
  self.postMessage('foobar', {yorkie: true});
  self.on('message', (data) => {
    console.log(data);
  });
}, {
  timeout: 5000,  // terminate after timeout
  defines: [],    // the parameters/objects passing to Worker
});

worker.postMessage({foo: 'bar'});
worker.onmessage = function(data) {
  // get message from worker
};

APIs

The node-webworker provides the class WebWorker for creating a separate worker.

Creating a webworker script

A WebWorker instance is corresponding a script to run, in low-level implementation, it creates a thread to sperate the v8 stack from the main.

The constructor WebWorker(script, options) accepts:

  • {Function} script the source code function which runs in worker. Will support the path to run.
  • {Object} options the options to config the worker
    • {Number} options.timeout the microsecond number to terminate forcily.
    • {Array} options.define the array of parameters to pass in worker context.

Messaging

In host environment, namely the Node.js, every WebWorker instance owns the method postMessage and the listener onmessage.

worker.postMessage('some data');
worker.onmessage = function(data) {
  // TODO
};

Correspondingly, in every worker context, it owns the same like the following:

const worker = new WebWorker(function(self) {
  self.onmessage = function(data) { .. };
  self.postMessage('some data sent from worker');
});

Collecting stdout and stderr

In worker context, the methods under console as console.log() and console.error are delegated by node-webworker which allows user to collect worker logs by worker as the following:

const worker = new WebWorker(function(self) {
  console.log('this sent to stdout');
  console.error('this sent to stderr');
});
worker.onstdout = function(msg) { ... };
worker.onstderr = function(msg) { ... };

Modular worker

In web worker, you can use the following builtin modules:

  • events - the Node.js official events mirror module
  • path - the Node.js official path mirror module

The other hand, you can also load your CommonJS file into your worker:

const worker = new WebWorker(function(self) {
  require('./tests/load-worker.js');
});

Note: The default path routing is from the process.cwd().

Installation

$ npm install webworker-ng --save

Tests

$ npm test

License

Apache License 2.0

node-webworker's People

Contributors

rokidbot avatar steambap avatar yorkie avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Forkers

steambap

node-webworker's Issues

安装报错

huangbinjie@~/workspace/yoi: (master) 🇨🇳 $ npm i webworker-ng

> [email protected] install /Users/huangbinjie/workspace/yoi/node_modules/webworker-ng
> node-gyp rebuild

  CXX(target) Release/obj.target/webworker/src/WebWorkerWrap.o
In file included from ../src/WebWorkerWrap.cc:1:
In file included from .././src/WebWorkerWrap.h:6:
In file included from ../node_modules/nan/nan.h:192:
../node_modules/nan/nan_maybe_43_inl.h:112:15: error: no member named 'ForceSet'
      in 'v8::Object'
  return obj->ForceSet(isolate->GetCurrentContext(), key, value, attribs);
         ~~~  ^
In file included from ../src/WebWorkerWrap.cc:1:
In file included from .././src/WebWorkerWrap.h:6:
../node_modules/nan/nan.h:834:18: warning: 'MakeCallback' is deprecated: Use
      MakeCallback(..., async_context) [-Wdeprecated-declarations]
    return node::MakeCallback(
                 ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:171:1: note: 
      'MakeCallback' has been explicitly marked deprecated here
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:88:20: note: expanded
      from macro 'NODE_DEPRECATED'
    __attribute__((deprecated(message))) declarator
                   ^
In file included from ../src/WebWorkerWrap.cc:1:
In file included from .././src/WebWorkerWrap.h:6:
../node_modules/nan/nan.h:849:18: warning: 'MakeCallback' is deprecated: Use
      MakeCallback(..., async_context) [-Wdeprecated-declarations]
    return node::MakeCallback(
                 ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:164:1: note: 
      'MakeCallback' has been explicitly marked deprecated here
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:88:20: note: expanded
      from macro 'NODE_DEPRECATED'
    __attribute__((deprecated(message))) declarator
                   ^
In file included from ../src/WebWorkerWrap.cc:1:
In file included from .././src/WebWorkerWrap.h:6:
../node_modules/nan/nan.h:864:18: warning: 'MakeCallback' is deprecated: Use
      MakeCallback(..., async_context) [-Wdeprecated-declarations]
    return node::MakeCallback(
                 ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:157:1: note: 
      'MakeCallback' has been explicitly marked deprecated here
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:88:20: note: expanded
      from macro 'NODE_DEPRECATED'
    __attribute__((deprecated(message))) declarator
                   ^
In file included from ../src/WebWorkerWrap.cc:1:
In file included from .././src/WebWorkerWrap.h:6:
../node_modules/nan/nan.h:1473:31: warning: 'MakeCallback' is deprecated: Use
      MakeCallback(..., async_context) [-Wdeprecated-declarations]
    return scope.Escape(node::MakeCallback(
                              ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:171:1: note: 
      'MakeCallback' has been explicitly marked deprecated here
NODE_DEPRECATED("Use MakeCallback(..., async_context)",
^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/node.h:88:20: note: expanded
      from macro 'NODE_DEPRECATED'
    __attribute__((deprecated(message))) declarator
                   ^
../src/WebWorkerWrap.cc:21:26: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    char* text = strdup(*String::Utf8Value(info[0]));
                         ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:33:30: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    char* pathname = strdup(*String::Utf8Value(info[0]));
                             ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:62:36: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    worker->request_name = strdup(*String::Utf8Value(info[0]));
                                   ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:63:36: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    worker->request_args = strdup(*String::Utf8Value(serializedParams.To...
                                   ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:97:24: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    char* id = strdup(*String::Utf8Value(info[0]));
                       ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:118:50: warning: 'Run' is deprecated
      [-Wdeprecated-declarations]
  Local<Value> result = script.ToLocalChecked()->Run();
                                                 ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:1250:3: note: 'Run' has
      been explicitly marked deprecated here
  V8_DEPRECATED("Use maybe version", Local<Value> Run());
  ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:141:58: warning: 'StackTrace' is deprecated
      [-Wdeprecated-declarations]
    Local<String> stack = Local<String>::Cast(try_catch->StackTrace());
                                                         ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:8328:3: note: 
      'StackTrace' has been explicitly marked deprecated here
  V8_DEPRECATED("Use maybe version.", Local<Value> StackTrace() const);
  ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:142:30: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
    fprintf(stderr, "%s\n", *String::Utf8Value(stack));
                             ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
../src/WebWorkerWrap.cc:358:33: warning: 'Utf8Value' is deprecated
      [-Wdeprecated-declarations]
  worker->callback_id = strdup(*String::Utf8Value(info[0]));
                                ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8.h:2821:5: note: 'Utf8Value'
      has been explicitly marked deprecated here
    V8_DEPRECATED("Use Isolate version",
    ^
/Users/huangbinjie/.node-gyp/10.0.0/include/node/v8config.h:321:29: note: 
      expanded from macro 'V8_DEPRECATED'
  declarator __attribute__((deprecated))
                            ^
13 warnings and 1 error generated.
make: *** [Release/obj.target/webworker/src/WebWorkerWrap.o] Error 1
gyp ERR! build error 
gyp ERR! stack Error: `make` failed with exit code: 2
gyp ERR! stack     at ChildProcess.onExit (/usr/local/lib/node_modules/npm/node_modules/node-gyp/lib/build.js:258:23)
gyp ERR! stack     at ChildProcess.emit (events.js:182:13)
gyp ERR! stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:225:12)
gyp ERR! System Darwin 17.5.0
gyp ERR! command "/usr/local/Cellar/node/10.0.0/bin/node" "/usr/local/lib/node_modules/npm/node_modules/node-gyp/bin/node-gyp.js" "rebuild"
gyp ERR! cwd /Users/huangbinjie/workspace/yoi/node_modules/webworker-ng
gyp ERR! node -v v10.0.0
gyp ERR! node-gyp -v v3.6.2
gyp ERR! not ok 
npm WARN [email protected] No repository field.

npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! [email protected] install: `node-gyp rebuild`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the [email protected] install script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     /Users/huangbinjie/.npm/_logs/2018-05-03T11_42_28_718Z-debug.log

顺便问一句,共享数据方便传送到 worker 吗?

how to call defines functions in worker

i am trying to call a function inside worker using defines option. i would like to use request as require in worker is not supported.

any example would help?

crash when doing terminate

Process 60651 launched: '/usr/local/bin/node' (x86_64)
TAP version 13
# single worker
ok 1 should be equal
Process 60651 stopped
* thread #7, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
    frame #0: 0x0000000100bcc88c node`worker + 54
node`worker:
->  0x100bcc88c <+54>: movq   %rax, (%rcx)
    0x100bcc88f <+57>: movq   (%rbx), %rax
    0x100bcc892 <+60>: movq   0x8(%rbx), %rcx
    0x100bcc896 <+64>: movq   %rcx, 0x8(%rax)
Target 0: (node) stopped.
(lldb) bt
* thread #7, stop reason = EXC_BAD_ACCESS (code=1, address=0x0)
  * frame #0: 0x0000000100bcc88c node`worker + 54
    frame #1: 0x00007fffc300e93b libsystem_pthread.dylib`_pthread_body + 180
    frame #2: 0x00007fffc300e887 libsystem_pthread.dylib`_pthread_start + 286
    frame #3: 0x00007fffc300e08d libsystem_pthread.dylib`thread_start + 13

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.