-
Notifications
You must be signed in to change notification settings - Fork 120
Add support for worker_threads #90
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
Open
sebamarynissen
wants to merge
11
commits into
rvagg:master
Choose a base branch
from
sebamarynissen:worker-threads
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 3 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
23d4dbb
Supper worker_threads
sebamarynissen 27308ba
Add .threaded function
sebamarynissen adf5891
Add threaded tests
sebamarynissen aa9b63f
Support transferList
sebamarynissen f6da7b3
Fix tests
sebamarynissen ea52a60
Add threaded example
sebamarynissen c4877c6
Update README.md
sebamarynissen 963fdad
Add transferList example
sebamarynissen 7a2c2a2
Update tests
sebamarynissen 91bafc1
Merge remote-tracking branch 'upstream/master'
sebamarynissen 8a274af
Merge onChild functionality
sebamarynissen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| 'use strict' | ||
| let $module | ||
| function handle (data, send) { | ||
| let idx = data.idx | ||
| , child = data.child | ||
| , method = data.method | ||
| , args = data.args | ||
| , callback = function () { | ||
| let _args = Array.prototype.slice.call(arguments) | ||
| if (_args[0] instanceof Error) { | ||
| let e = _args[0] | ||
| _args[0] = { | ||
| '$error' : '$error' | ||
| , 'type' : e.constructor.name | ||
| , 'message' : e.message | ||
| , 'stack' : e.stack | ||
| } | ||
| Object.keys(e).forEach(function(key) { | ||
| _args[0][key] = e[key] | ||
| }) | ||
| } | ||
| send({ idx: idx, child: child, args: _args }) | ||
| } | ||
| , exec | ||
|
|
||
| if (method == null && typeof $module == 'function') | ||
| exec = $module | ||
| else if (typeof $module[method] == 'function') | ||
| exec = $module[method] | ||
|
|
||
| if (!exec) | ||
| return console.error('NO SUCH METHOD:', method) | ||
|
|
||
| exec.apply(null, args.concat([ callback ])) | ||
| } | ||
|
|
||
| module.exports = function(send) { | ||
| return function(data) { | ||
| if (!$module) return $module = require(data.module) | ||
| if (data == 'die') return process.exit(0) | ||
| handle(data, send) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,52 +1,10 @@ | ||
| 'use strict' | ||
|
|
||
| let $module | ||
| const handle = require('./handle') | ||
|
|
||
| /* | ||
| let contextProto = this.context; | ||
| while (contextProto = Object.getPrototypeOf(contextProto)) { | ||
| completionGroups.push(Object.getOwnPropertyNames(contextProto)); | ||
| } | ||
| */ | ||
|
|
||
|
|
||
| function handle (data) { | ||
| let idx = data.idx | ||
| , child = data.child | ||
| , method = data.method | ||
| , args = data.args | ||
| , callback = function () { | ||
| let _args = Array.prototype.slice.call(arguments) | ||
| if (_args[0] instanceof Error) { | ||
| let e = _args[0] | ||
| _args[0] = { | ||
| '$error' : '$error' | ||
| , 'type' : e.constructor.name | ||
| , 'message' : e.message | ||
| , 'stack' : e.stack | ||
| } | ||
| Object.keys(e).forEach(function(key) { | ||
| _args[0][key] = e[key] | ||
| }) | ||
| } | ||
| process.send({ idx: idx, child: child, args: _args }) | ||
| } | ||
| , exec | ||
|
|
||
| if (method == null && typeof $module == 'function') | ||
| exec = $module | ||
| else if (typeof $module[method] == 'function') | ||
| exec = $module[method] | ||
|
|
||
| if (!exec) | ||
| return console.error('NO SUCH METHOD:', method) | ||
|
|
||
| exec.apply(null, args.concat([ callback ])) | ||
| } | ||
|
|
||
|
|
||
| process.on('message', function (data) { | ||
| if (!$module) return $module = require(data.module) | ||
| if (data == 'die') return process.exit(0) | ||
| handle(data) | ||
| }) | ||
| process.on('message', handle(process.send.bind(process))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| 'use strict' | ||
| const handle = require('./handle') | ||
| const {parentPort} = require('worker_threads') | ||
| parentPort.on('message', handle(parentPort.postMessage.bind(parentPort))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,27 @@ | |
| const childProcess = require('child_process') | ||
| , childModule = require.resolve('./child/index') | ||
|
|
||
| let checked, available | ||
| function fork (forkModule, workerOptions, worker_threads) { | ||
|
|
||
| // Check whether we need to run using worker_threads. | ||
| if (worker_threads) { | ||
|
|
||
| if (!checked) { | ||
| try { | ||
| require('worker_threads') | ||
| available = true | ||
| } catch (e) { | ||
| available = false | ||
| console.warn('[WARNING] Worker threads are not available! Make sure you run node 10.x or higher with --experimental-worker!') | ||
|
sebamarynissen marked this conversation as resolved.
Outdated
|
||
| } | ||
| checked = true | ||
| } | ||
|
|
||
| if (available) return thread(forkModule, workerOptions); | ||
|
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. break a newline here
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. and swallow the blank line underneath |
||
|
|
||
| } | ||
|
|
||
| function fork (forkModule, workerOptions) { | ||
| // suppress --debug / --inspect flags while preserving others (like --harmony) | ||
| let filteredArgs = process.execArgv.filter(function (v) { | ||
| return !(/^--(debug|inspect)/).test(v) | ||
|
|
@@ -29,5 +48,15 @@ function fork (forkModule, workerOptions) { | |
| } | ||
| } | ||
|
|
||
| function thread (forkModule, workerOptions) { | ||
| const {Worker} = require('worker_threads'); | ||
| let child = new Worker(require.resolve('./child/thread'), workerOptions) | ||
| child.on('error', function() {}) | ||
| child.postMessage({ module: forkModule }) | ||
| return { | ||
| send : child.postMessage.bind(child) | ||
| , child : child | ||
| } | ||
| } | ||
|
|
||
| module.exports = fork | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| const test = require('.') | ||
| const workerFarm = require('..') | ||
| test(workerFarm) | ||
| test(workerFarm.threaded) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I get why you've gone with this name but I think I'd prefer it be
workerThreads