-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support multiple worker functions per file
Also fixes some coverage issues
- Loading branch information
Showing
13 changed files
with
218 additions
and
25 deletions.
There are no files selected for viewing
This file contains 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 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 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,17 @@ | ||
'use strict'; | ||
|
||
const Piscina = require('../..'); | ||
const { resolve } = require('path'); | ||
|
||
// It is possible for a single Piscina pool to run multiple | ||
// workers at the same time. To do so, pass the worker filename | ||
// to runTask rather than to the Piscina constructor. | ||
|
||
const pool = new Piscina({ filename: resolve(__dirname, 'worker.js') }); | ||
|
||
(async () => { | ||
console.log(await Promise.all([ | ||
pool.run({ a: 2, b: 3 }, { name: 'add' }), | ||
pool.run({ a: 2, b: 3 }, { name: 'multiply' }) | ||
])); | ||
})(); |
This file contains 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,10 @@ | ||
import { Piscina } from 'piscina'; | ||
|
||
const pool = new Piscina({ | ||
filename: new URL('./worker.mjs', import.meta.url).href | ||
}); | ||
|
||
console.log(await Promise.all([ | ||
pool.run({ a: 2, b: 3 }, { name: 'add' }), | ||
pool.run({ a: 2, b: 3 }, { name: 'multiply' }) | ||
])); |
This file contains 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,11 @@ | ||
'use strict'; | ||
|
||
function add ({ a, b }) { return a + b; } | ||
function multiply ({ a, b }) { return a * b; }; | ||
|
||
add.add = add; | ||
add.multiply = multiply; | ||
|
||
// The add function is the default handler, but | ||
// additional named handlers are exported. | ||
module.exports = add; |
This file contains 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 @@ | ||
export function add ({ a, b }) { return a + b; } | ||
export function multiply ({ a, b }) { return a * b; }; | ||
|
||
export default add; |
This file contains 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
Oops, something went wrong.