multithreading - Javascript Worker Thread Options? -
i developing single page application using electron (io.js + chrome). application interfaces sdk requires polling/heartbeat run main/event loop (similar libusb
, libusb_handle_events_timeout_completed
api). hoping use worker
thread , timer seems sandboxing may have made option impossible--unless missing something. i've tested timer functionality within worker thread , works great. however, need require
various other modules (ffi, etc) allow me communicate sdk; however, require
not available worker
threads.
are there other options/apis/modules/etc available allow me start new thread can require
other modules can make simple call sdk every many milliseconds? long have ipc main thread, new thread can sole communicator sdk, therefore there should no problems corruption. suggestions or further areas research appreciated.
after spending more time on problem found node.js clusters best approach me. have ability to:
require
code.- still have nice neat ipc.
- can monitor lifecycle of forked cluster (process).
one thing did learn. learned couple things:
- if use electron, do, make sure use
cluster.setupmaster()
otherwise when cluster if forked attempt use electron environment throw bunch ofbad option
errors ,cluster.fork
fail. - one other problem found if app not have terminal , use
console.log
within worker cluster, cluster terminate. not sure if specific electron (io.js) or what, took time figure out. app runs, , logs fine terminal. without terminal terminate worker. since first line of workerconsole.log
, made harder find.
there may better solutions, seems work best use case.
for interested simplified implementation looks this:
app.js
var cluster = require('cluster'); var path = require('path'); var sdkworker = null; // must use setupmaster otherwise default electron // environment used , fail with: // ../node_modules/electron-prebuilt/dist/electron: bad option: --type=renderer // ... cluster.setupmaster({ exec: path.join(__dirname, "sdk_worker.js"), //args: ['--use', 'https'], silent: false }); sdkworker = cluster.fork({}); cluster.on('fork', function(worker) { console.log('worker: forked: ' + worker.process.pid); }); cluster.on('online', function(worker) { console.log('worker: ' + worker.process.pid + ' online'); }); cluster.on('exit', function(worker, code, signal) { var suicide = ""; if (worker.suicide === true) { suicide = " [suicide] "; } console.log('exit: worker: '+suicide+ worker.process.pid + ' died code: ' + code + ', , signal: ' + signal); sdkworker = null; //console.log('starting new worker'); //this.sdkworker = cluster.fork(); }); sdkworker.on( 'message', function( msg ) { console.log("from sdk: "+msg.cmdid); } ); // init lib , sdk platform sdkworker.send( {cmdid:"init"} ); // initial enumeration of usb sdkworker.send( {cmdid:"enumusb"} ); // start contant polling of sdk sdkworker.send( {cmdid:"startpoll"} );
sdk_worker.js
// cluster support process.on('message', function(msg) { //console.log("from master: "+msg.cmdid); //////// bad });
Comments
Post a Comment