forked from nodejscn/node-api-cn
-
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.
- Loading branch information
Showing
129 changed files
with
1,065 additions
and
479 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
File renamed without changes.
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 |
---|---|---|
|
@@ -4,5 +4,4 @@ | |
> 稳定性: 2 - 稳定 | ||
|
||
`assert` 模块提供了一组简单的断言测试,可用于测试不变量。 | ||
该模块提供了建议的[严格模式][`strict` mode]和更宽松的遗留模式。 | ||
|
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
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
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
129 changes: 129 additions & 0 deletions
129
async_hooks/using_asyncresource_for_a_worker_thread_pool.md
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,129 @@ | ||
|
||
The following example shows how to use the `AsyncResource` class to properly | ||
provide async tracking for a [`Worker`][] pool. Other resource pools, such as | ||
database connection pools, can follow a similar model. | ||
|
||
Assuming that the task is adding two numbers, using a file named | ||
`task_processor.js` with the following content: | ||
|
||
```js | ||
const { parentPort } = require('worker_threads'); | ||
parentPort.on('message', (task) => { | ||
parentPort.postMessage(task.a + task.b); | ||
}); | ||
``` | ||
|
||
a Worker pool around it could use the following structure: | ||
|
||
```js | ||
const { AsyncResource } = require('async_hooks'); | ||
const { EventEmitter } = require('events'); | ||
const path = require('path'); | ||
const { Worker } = require('worker_threads'); | ||
const kTaskInfo = Symbol('kTaskInfo'); | ||
const kWorkerFreedEvent = Symbol('kWorkerFreedEvent'); | ||
class WorkerPoolTaskInfo extends AsyncResource { | ||
constructor(callback) { | ||
super('WorkerPoolTaskInfo'); | ||
this.callback = callback; | ||
} | ||
done(err, result) { | ||
this.runInAsyncScope(this.callback, null, err, result); | ||
this.emitDestroy(); // `TaskInfo`s are used only once. | ||
} | ||
} | ||
class WorkerPool extends EventEmitter { | ||
constructor(numThreads) { | ||
super(); | ||
this.numThreads = numThreads; | ||
this.workers = []; | ||
this.freeWorkers = []; | ||
for (let i = 0; i < numThreads; i++) | ||
this.addNewWorker(); | ||
} | ||
addNewWorker() { | ||
const worker = new Worker(path.resolve(__dirname, 'task_processor.js')); | ||
worker.on('message', (result) => { | ||
// In case of success: Call the callback that was passed to `runTask`, | ||
// remove the `TaskInfo` associated with the Worker, and mark it as free | ||
// again. | ||
worker[kTaskInfo].done(null, result); | ||
worker[kTaskInfo] = null; | ||
this.freeWorkers.push(worker); | ||
this.emit(kWorkerFreedEvent); | ||
}); | ||
worker.on('error', (err) => { | ||
// In case of an uncaught exception: Call the callback that was passed to | ||
// `runTask` with the error. | ||
if (worker[kTaskInfo]) | ||
worker[kTaskInfo].done(err, null); | ||
else | ||
this.emit('error', err); | ||
// Remove the worker from the list and start a new Worker to replace the | ||
// current one. | ||
this.workers.splice(this.workers.indexOf(worker), 1); | ||
this.addNewWorker(); | ||
}); | ||
this.workers.push(worker); | ||
this.freeWorkers.push(worker); | ||
} | ||
runTask(task, callback) { | ||
if (this.freeWorkers.length === 0) { | ||
// No free threads, wait until a worker thread becomes free. | ||
this.once(kWorkerFreedEvent, () => this.runTask(task, callback)); | ||
return; | ||
} | ||
const worker = this.freeWorkers.pop(); | ||
worker[kTaskInfo] = new WorkerPoolTaskInfo(callback); | ||
worker.postMessage(task); | ||
} | ||
close() { | ||
for (const worker of this.workers) worker.terminate(); | ||
} | ||
} | ||
module.exports = WorkerPool; | ||
``` | ||
|
||
Without the explicit tracking added by the `WorkerPoolTaskInfo` objects, | ||
it would appear that the callbacks are associated with the individual `Worker` | ||
objects. However, the creation of the `Worker`s is not associated with the | ||
creation of the tasks and does not provide information about when tasks | ||
were scheduled. | ||
|
||
This pool could be used as follows: | ||
|
||
```js | ||
const WorkerPool = require('./worker_pool.js'); | ||
const os = require('os'); | ||
const pool = new WorkerPool(os.cpus().length); | ||
let finished = 0; | ||
for (let i = 0; i < 10; i++) { | ||
pool.runTask({ a: 42, b: 100 }, (err, result) => { | ||
console.log(i, err, result); | ||
if (++finished === 10) | ||
pool.close(); | ||
}); | ||
} | ||
``` | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
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
Oops, something went wrong.