forked from piscinajs/piscina
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add communication from worker to main thread
- Loading branch information
Showing
9 changed files
with
65 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
'use strict'; | ||
|
||
const Piscina = require('../../dist/src'); | ||
const { resolve } = require('path'); | ||
|
||
const piscina = new Piscina({ | ||
filename: resolve(__dirname, 'worker.js') | ||
}); | ||
|
||
(async function () { | ||
piscina.on('message', (event) => { | ||
console.log("Messsage received from worker: ", event); | ||
}); | ||
|
||
await piscina.run(); | ||
})(); |
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,6 @@ | ||
'use strict'; | ||
const { parentPort } = require('worker_threads') | ||
|
||
module.exports = () => { | ||
parentPort.postMessage('hello from the worker pool'); | ||
}; |
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
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,19 @@ | ||
import Piscina from '../dist/src'; | ||
import { test } from 'tap'; | ||
import { resolve } from 'path'; | ||
import { once } from 'events'; | ||
|
||
test('Pool receive message from workers', async ({ equal }) => { | ||
const pool = new Piscina({ | ||
filename: resolve(__dirname, 'fixtures/eval.js') | ||
}); | ||
|
||
const messagePromise = once(pool, 'message'); | ||
|
||
const taskResult = pool.runTask(` | ||
require('worker_threads').parentPort.postMessage("some message"); | ||
42 | ||
`); | ||
equal(await taskResult, 42); | ||
equal((await messagePromise)[0], 'some message'); | ||
}); |
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