You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+63-31
Original file line number
Diff line number
Diff line change
@@ -197,6 +197,7 @@ The following options are available:
197
197
- In case of `'process'`, `child_process` will be used. Only available in a node.js environment.
198
198
- In case of `'thread'`, `worker_threads` will be used. If `worker_threads` are not available, an error is thrown. Only available in a node.js environment.
199
199
-`workerTerminateTimeout: number`. The timeout in milliseconds to wait for a worker to cleanup it's resources on termination before stopping it forcefully. Default value is `1000`.
200
+
-`abortListenerTimeout: number`. The timeout in milliseconds to wait for abort listener's before stopping it forcefully, triggering cleanup. Default value is `1000`.
200
201
-`forkArgs: String[]`. For `process` worker type. An array passed as `args` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options)
201
202
-`forkOpts: Object`. For `process` worker type. An object passed as `options` to [child_process.fork](https://nodejs.org/api/child_process.html#child_processforkmodulepath-args-options). See nodejs documentation for available options.
202
203
-`workerOpts: Object`. For `web` worker type. An object passed to the [constructor of the web worker](https://html.spec.whatwg.org/multipage/workers.html#dom-worker). See [WorkerOptions specification](https://html.spec.whatwg.org/multipage/workers.html#workeroptions) for available options.
@@ -393,7 +394,62 @@ workerpool.worker({
393
394
});
394
395
```
395
396
396
-
Tasks may configure an `abort handler` to perform cleanup operations when `timeout` or `cancel` is called on a `task`. the `abortListenerTimeout` option can be configured to control when cleanup should be aborted in the case an `abortHandler` never resolves. This timeout trigger will cause the given worker to be cleaned up. Allowing a new worker to be created if need be.
397
+
### Events
398
+
399
+
You can send data back from workers to the pool while the task is being executed using the `workerEmit` function:
400
+
401
+
`workerEmit(payload: any) : unknown`
402
+
403
+
This function only works inside a worker **and** during a task.
404
+
405
+
Example:
406
+
407
+
```js
408
+
// file myWorker.js
409
+
constworkerpool=require('workerpool');
410
+
411
+
functioneventExample(delay) {
412
+
workerpool.workerEmit({
413
+
status:'in_progress',
414
+
});
415
+
416
+
workerpool.workerEmit({
417
+
status:'complete',
418
+
});
419
+
420
+
returntrue;
421
+
}
422
+
423
+
// create a worker and register functions
424
+
workerpool.worker({
425
+
eventExample: eventExample,
426
+
});
427
+
```
428
+
429
+
To receive those events, you can use the `on` option of the pool `exec` method:
430
+
431
+
```js
432
+
pool.exec('eventExample', [], {
433
+
on:function (payload) {
434
+
if (payload.status==='in_progress') {
435
+
console.log('In progress...');
436
+
} elseif (payload.status==='complete') {
437
+
console.log('Done!');
438
+
}
439
+
},
440
+
});
441
+
```
442
+
443
+
### Worker API
444
+
Workers have access to a `worker` api which contains the following methods
Worker termination may be recoverable through `abort listeners` which are registered through `worker.addAbortListener`. If all registered listeners resolve then the worker will not be terminated, allowing for worker reuse in some cases.
451
+
452
+
NOTE: For operations to successfully clean up, a worker implementation should be *async*. If the worker thread is blocked, then the worker will be killed.
397
453
398
454
```js
399
455
functionasyncTimeout() {
@@ -402,11 +458,9 @@ function asyncTimeout() {
402
458
let timeout =setTimeout(() => {
403
459
resolve();
404
460
}, 5000);
405
-
406
-
// An abort listener allows for cleanup for a given worker
407
-
// such that it may be resused for future tasks
408
-
// if an execption is thrown within scope of the handler
409
-
// the worker instance will be destroyed.
461
+
462
+
// Register a listener which will resolve before the time out
463
+
// above triggers.
410
464
me.worker.addAbortListener(asyncfunction () {
411
465
clearTimeout(timeout);
412
466
resolve();
@@ -425,25 +479,17 @@ workerpool.worker(
425
479
);
426
480
```
427
481
428
-
### Events
429
-
430
-
You can send data back from workers to the pool while the task is being executed using the `workerEmit` function:
431
-
432
-
`workerEmit(payload: any) : unknown`
433
-
434
-
This function only works inside a worker **and** during a task.
435
482
436
-
Example:
483
+
Events may also be emitted from the `worker` api through `worker.emit`
437
484
438
485
```js
439
486
// file myWorker.js
440
487
constworkerpool=require('workerpool');
441
488
442
489
functioneventExample(delay) {
443
-
workerpool.workerEmit({
444
-
status:'in_progress',
490
+
this.worker.emit({
491
+
status:"in_progress",
445
492
});
446
-
447
493
workerpool.workerEmit({
448
494
status:'complete',
449
495
});
@@ -457,20 +503,6 @@ workerpool.worker({
457
503
});
458
504
```
459
505
460
-
To receive those events, you can use the `on` option of the pool `exec` method:
461
-
462
-
```js
463
-
pool.exec('eventExample', [], {
464
-
on:function (payload) {
465
-
if (payload.status==='in_progress') {
466
-
console.log('In progress...');
467
-
} elseif (payload.status==='complete') {
468
-
console.log('Done!');
469
-
}
470
-
},
471
-
});
472
-
```
473
-
474
506
### Utilities
475
507
476
508
Following properties are available for convenience:
Copy file name to clipboardExpand all lines: src/worker.js
+8-3
Original file line number
Diff line number
Diff line change
@@ -35,13 +35,18 @@ var worker = {
35
35
// works in both node.js and the browser
36
36
varpublicWorker={
37
37
/**
38
-
*
39
-
* @param {() => Promise<void>} listener
40
-
*/
38
+
* Registers listeners which will trigger when a task is timed out or cancled. If all listeners resolve, the worker executing the given task will not be terminated.
39
+
* *Note*: If there is a blocking operation within a listener, the worker will be terminated.
40
+
* @param {() => Promise<void>} listener
41
+
*/
41
42
addAbortListener: function(listener){
42
43
worker.abortListeners.push(listener);
43
44
},
44
45
46
+
/**
47
+
* Emit an event from the worker thread to the main thread.
0 commit comments