multi-tasks 1.2.7 → 1.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -21
- package/README.md +98 -97
- package/examples/example0/run.js +37 -37
- package/examples/example1/RunnerConsumer.js +25 -25
- package/examples/example1/RunnerProducer.js +34 -34
- package/examples/example1/run.js +8 -8
- package/examples/example2/RunnerConsumer.js +39 -39
- package/examples/example2/RunnerProducer.js +34 -34
- package/examples/example2/run.js +22 -22
- package/examples/example3-dynamicIncreasing/RunnerConsumer.js +175 -175
- package/examples/example3-dynamicIncreasing/RunnerProducer.js +48 -48
- package/examples/example3-dynamicIncreasing/run.js +35 -35
- package/examples/example3-dynamicIncreasing/util.js +45 -45
- package/index.js +36 -36
- package/package.json +22 -22
- package/test/splitter.test.js +16 -16
- package/utils/makedir.js +28 -23
- package/utils/test_makedir.js +2 -2
- package/workers/SuperConsumer.js +105 -105
- package/workers/SuperProducer.js +96 -96
- package/workers/master.js +170 -170
- package/workers/report/report_status.js +51 -51
- package/workers/taskSplitter.js +29 -29
- package/workers/test/MyConsumer.js +24 -24
- package/workers/test/MyProducer.js +25 -25
- package/workers/test/test_master.js +11 -11
- package/workers/test/test_producer.js +3 -3
- package/workers/test/test_splitter.js +6 -6
package/LICENSE
CHANGED
|
@@ -1,21 +1,21 @@
|
|
|
1
|
-
MIT License
|
|
2
|
-
|
|
3
|
-
Copyright (c) 2024 Lei Zhang
|
|
4
|
-
|
|
5
|
-
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
-
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
-
in the Software without restriction, including without limitation the rights
|
|
8
|
-
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
-
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
-
furnished to do so, subject to the following conditions:
|
|
11
|
-
|
|
12
|
-
The above copyright notice and this permission notice shall be included in all
|
|
13
|
-
copies or substantial portions of the Software.
|
|
14
|
-
|
|
15
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
-
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
-
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
-
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
-
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
-
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
-
SOFTWARE.
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Lei Zhang
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,97 +1,98 @@
|
|
|
1
|
-
# multi-tasks
|
|
2
|
-
multi-tasks
|
|
3
|
-
|
|
4
|
-
Install:
|
|
5
|
-
|
|
6
|
-
```javascript
|
|
7
|
-
npm install multi-tasks
|
|
8
|
-
```
|
|
9
|
-
|
|
10
|
-
How to use:
|
|
11
|
-
|
|
12
|
-
```javascript
|
|
13
|
-
//see examples/example0
|
|
14
|
-
let multiTasks = require('multi-tasks').multiTasks;
|
|
15
|
-
//You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
16
|
-
let alltasks = [];
|
|
17
|
-
for(let i=0;i<100;i++){
|
|
18
|
-
let task_props = {
|
|
19
|
-
index: i,
|
|
20
|
-
name: `task-${i}`,
|
|
21
|
-
description: `This prop is for a subtask`
|
|
22
|
-
};
|
|
23
|
-
alltasks.push(task_props);
|
|
24
|
-
}
|
|
25
|
-
|
|
26
|
-
//You need to provide a process function to handle a certain sub-task and return the result data
|
|
27
|
-
let processTask = (task)=>{
|
|
28
|
-
console.log('this is the subtask data you created above:', task);
|
|
29
|
-
let {index} = task;
|
|
30
|
-
|
|
31
|
-
//any exceptions will be captured
|
|
32
|
-
if(index===3) throw 'exception';
|
|
33
|
-
if(index===4) return Promise.reject({err:'a test error'});
|
|
34
|
-
|
|
35
|
-
//the returned data will be saved
|
|
36
|
-
return Promise.resolve({
|
|
37
|
-
message: 'this is a result from a demo, random data=' + Math.random()
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
multiTasks({
|
|
42
|
-
tasks: alltasks,
|
|
43
|
-
processTask,
|
|
44
|
-
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
45
|
-
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
46
|
-
numberOfWorkers: 6, //Assign how many workers are working in parallel, default is the number of cpu cores minus one
|
|
47
|
-
onFinish: (report)=>{
|
|
48
|
-
console.log(report);//the report can be viewed here
|
|
49
|
-
}
|
|
50
|
-
});
|
|
51
|
-
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Or the older way, to provide the Producer & Consumer
|
|
55
|
-
|
|
56
|
-
```javascript
|
|
57
|
-
let master = require('multi-tasks').master;
|
|
58
|
-
let RunnerProducer;//See examples, please provide a Producer class to specify all tasks you have, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
59
|
-
let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
|
|
60
|
-
|
|
61
|
-
master.start(RunnerProducer, RunnerConsumer, {
|
|
62
|
-
multi_task_parent_folder,
|
|
63
|
-
quotaOfEachTask,
|
|
64
|
-
numberOfWorkers,
|
|
65
|
-
onFinish
|
|
66
|
-
});
|
|
67
|
-
```
|
|
68
|
-
|
|
69
|
-
Have a try:
|
|
70
|
-
|
|
71
|
-
```shell
|
|
72
|
-
node examples/example0/run
|
|
73
|
-
node examples/example1/run
|
|
74
|
-
```
|
|
75
|
-
|
|
76
|
-
Changelog:
|
|
77
|
-
|
|
78
|
-
- 1.2.
|
|
79
|
-
- 1.2.
|
|
80
|
-
- 1.2.
|
|
81
|
-
- 1.2.
|
|
82
|
-
- 1.2.
|
|
83
|
-
- 1.2.
|
|
84
|
-
- 1.2.
|
|
85
|
-
- 1.2.
|
|
86
|
-
- 1.
|
|
87
|
-
- 1.1.
|
|
88
|
-
- 1.1.
|
|
89
|
-
- 1.1.
|
|
90
|
-
- 1.1.
|
|
91
|
-
- 1.0
|
|
92
|
-
- 1.0.
|
|
93
|
-
- 1.0.
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
1
|
+
# multi-tasks
|
|
2
|
+
multi-tasks
|
|
3
|
+
|
|
4
|
+
Install:
|
|
5
|
+
|
|
6
|
+
```javascript
|
|
7
|
+
npm install multi-tasks
|
|
8
|
+
```
|
|
9
|
+
|
|
10
|
+
How to use:
|
|
11
|
+
|
|
12
|
+
```javascript
|
|
13
|
+
//see examples/example0
|
|
14
|
+
let multiTasks = require('multi-tasks').multiTasks;
|
|
15
|
+
//You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
16
|
+
let alltasks = [];
|
|
17
|
+
for(let i=0;i<100;i++){
|
|
18
|
+
let task_props = {
|
|
19
|
+
index: i,
|
|
20
|
+
name: `task-${i}`,
|
|
21
|
+
description: `This prop is for a subtask`
|
|
22
|
+
};
|
|
23
|
+
alltasks.push(task_props);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
//You need to provide a process function to handle a certain sub-task and return the result data
|
|
27
|
+
let processTask = (task)=>{
|
|
28
|
+
console.log('this is the subtask data you created above:', task);
|
|
29
|
+
let {index} = task;
|
|
30
|
+
|
|
31
|
+
//any exceptions will be captured
|
|
32
|
+
if(index===3) throw 'exception';
|
|
33
|
+
if(index===4) return Promise.reject({err:'a test error'});
|
|
34
|
+
|
|
35
|
+
//the returned data will be saved
|
|
36
|
+
return Promise.resolve({
|
|
37
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
38
|
+
});
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
multiTasks({
|
|
42
|
+
tasks: alltasks,
|
|
43
|
+
processTask,
|
|
44
|
+
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
45
|
+
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
46
|
+
numberOfWorkers: 6, //Assign how many workers are working in parallel, default is the number of cpu cores minus one
|
|
47
|
+
onFinish: (report)=>{
|
|
48
|
+
console.log(report);//the report can be viewed here
|
|
49
|
+
}
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Or the older way, to provide the Producer & Consumer
|
|
55
|
+
|
|
56
|
+
```javascript
|
|
57
|
+
let master = require('multi-tasks').master;
|
|
58
|
+
let RunnerProducer;//See examples, please provide a Producer class to specify all tasks you have, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
59
|
+
let RunnerConsumer;//See examples, please provide a Consumer class to process a certain sub-task and return the processed data
|
|
60
|
+
|
|
61
|
+
master.start(RunnerProducer, RunnerConsumer, {
|
|
62
|
+
multi_task_parent_folder,
|
|
63
|
+
quotaOfEachTask,
|
|
64
|
+
numberOfWorkers,
|
|
65
|
+
onFinish
|
|
66
|
+
});
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Have a try:
|
|
70
|
+
|
|
71
|
+
```shell
|
|
72
|
+
node examples/example0/run
|
|
73
|
+
node examples/example1/run
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
Changelog:
|
|
77
|
+
|
|
78
|
+
- 1.2.8 Fix: create task folder failed on MacOS
|
|
79
|
+
- 1.2.7 Small updates
|
|
80
|
+
- 1.2.6 Support onFinish event
|
|
81
|
+
- 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
|
|
82
|
+
- 1.2.4 Fix: opt.numberOfWorkers not work
|
|
83
|
+
- 1.2.3 Update README
|
|
84
|
+
- 1.2.2 Update README and examples
|
|
85
|
+
- 1.2.1 Handle exceptions and errors in subtasks
|
|
86
|
+
- 1.2.0 Simplified usage by providing the function way and support return Promise
|
|
87
|
+
- 1.1.4 Remove make-dir
|
|
88
|
+
- 1.1.3 Simplified usage, see example0
|
|
89
|
+
- 1.1.2
|
|
90
|
+
- 1.1.1 Rename files, updated changelog
|
|
91
|
+
- 1.1.0 Simplified the usage of a customized Consumer, see example0
|
|
92
|
+
- 1.0.8 Fix examples
|
|
93
|
+
- 1.0.7 Remove moment
|
|
94
|
+
- 1.0.6 Performance optimization
|
|
95
|
+
|
|
96
|
+
Github:
|
|
97
|
+
|
|
98
|
+
[https://github.com/zhanglei923/multi-tasks](https://github.com/zhanglei923/multi-tasks)
|
package/examples/example0/run.js
CHANGED
|
@@ -1,38 +1,38 @@
|
|
|
1
|
-
let multiTasks = require('../../index').multiTasks;
|
|
2
|
-
|
|
3
|
-
//You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
4
|
-
let alltasks = [];
|
|
5
|
-
for(let i=0;i<100;i++){
|
|
6
|
-
let task_props = {
|
|
7
|
-
index: i,
|
|
8
|
-
name: `task-${i}`,
|
|
9
|
-
description: `This prop is for a subtask`
|
|
10
|
-
};
|
|
11
|
-
alltasks.push(task_props);
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
//You need to provide a process function to handle a certain sub-task and return the result data
|
|
15
|
-
let processTask = (task)=>{
|
|
16
|
-
console.log('this is the subtask data you created above:', task);
|
|
17
|
-
let {index} = task;
|
|
18
|
-
|
|
19
|
-
//any exceptions will be captured
|
|
20
|
-
if(index===3) throw 'exception';
|
|
21
|
-
if(index===4) return Promise.reject({err:'a test error'});
|
|
22
|
-
|
|
23
|
-
//the returned data will be saved
|
|
24
|
-
return Promise.resolve({
|
|
25
|
-
message: 'this is a result from a demo, random data=' + Math.random()
|
|
26
|
-
});
|
|
27
|
-
}
|
|
28
|
-
|
|
29
|
-
multiTasks({
|
|
30
|
-
tasks: alltasks,
|
|
31
|
-
processTask,
|
|
32
|
-
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
33
|
-
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
34
|
-
numberOfWorkers: 6, //Assign how many workers are working in parallel
|
|
35
|
-
onFinish: (report)=>{
|
|
36
|
-
console.log(report);
|
|
37
|
-
}
|
|
1
|
+
let multiTasks = require('../../index').multiTasks;
|
|
2
|
+
|
|
3
|
+
//You need to provide the data of all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
4
|
+
let alltasks = [];
|
|
5
|
+
for(let i=0;i<100;i++){
|
|
6
|
+
let task_props = {
|
|
7
|
+
index: i,
|
|
8
|
+
name: `task-${i}`,
|
|
9
|
+
description: `This prop is for a subtask`
|
|
10
|
+
};
|
|
11
|
+
alltasks.push(task_props);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
//You need to provide a process function to handle a certain sub-task and return the result data
|
|
15
|
+
let processTask = (task)=>{
|
|
16
|
+
console.log('this is the subtask data you created above:', task);
|
|
17
|
+
let {index} = task;
|
|
18
|
+
|
|
19
|
+
//any exceptions will be captured
|
|
20
|
+
if(index===3) throw 'exception';
|
|
21
|
+
if(index===4) return Promise.reject({err:'a test error'});
|
|
22
|
+
|
|
23
|
+
//the returned data will be saved
|
|
24
|
+
return Promise.resolve({
|
|
25
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
multiTasks({
|
|
30
|
+
tasks: alltasks,
|
|
31
|
+
processTask,
|
|
32
|
+
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
33
|
+
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
34
|
+
numberOfWorkers: 6, //Assign how many workers are working in parallel
|
|
35
|
+
onFinish: (report)=>{
|
|
36
|
+
console.log(report);
|
|
37
|
+
}
|
|
38
38
|
});
|
|
@@ -1,26 +1,26 @@
|
|
|
1
|
-
let SuperConsumer = require('../../index').SuperConsumer;
|
|
2
|
-
|
|
3
|
-
class TestConsumer extends SuperConsumer {
|
|
4
|
-
|
|
5
|
-
processTask(actionData){
|
|
6
|
-
console.log(`processing data:`, actionData);
|
|
7
|
-
//you can also return a data instead of a promise object
|
|
8
|
-
let {index} = actionData;
|
|
9
|
-
if(index===3) throw 'exception';
|
|
10
|
-
if(index===4) return Promise.reject({err:'a test error'});
|
|
11
|
-
return new Promise((resolve) => {
|
|
12
|
-
setTimeout(() => {
|
|
13
|
-
let result = {
|
|
14
|
-
task:{
|
|
15
|
-
...actionData,
|
|
16
|
-
},
|
|
17
|
-
result: {
|
|
18
|
-
message: 'this is a result from a demo, random data=' + Math.random()
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
resolve(result);
|
|
22
|
-
}, 100);
|
|
23
|
-
});
|
|
24
|
-
}
|
|
25
|
-
}
|
|
1
|
+
let SuperConsumer = require('../../index').SuperConsumer;
|
|
2
|
+
|
|
3
|
+
class TestConsumer extends SuperConsumer {
|
|
4
|
+
|
|
5
|
+
processTask(actionData){
|
|
6
|
+
console.log(`processing data:`, actionData);
|
|
7
|
+
//you can also return a data instead of a promise object
|
|
8
|
+
let {index} = actionData;
|
|
9
|
+
if(index===3) throw 'exception';
|
|
10
|
+
if(index===4) return Promise.reject({err:'a test error'});
|
|
11
|
+
return new Promise((resolve) => {
|
|
12
|
+
setTimeout(() => {
|
|
13
|
+
let result = {
|
|
14
|
+
task:{
|
|
15
|
+
...actionData,
|
|
16
|
+
},
|
|
17
|
+
result: {
|
|
18
|
+
message: 'this is a result from a demo, random data=' + Math.random()
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
resolve(result);
|
|
22
|
+
}, 100);
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
26
|
module.exports = TestConsumer;
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
let SuperProducer = require('../../index').SuperProducer;
|
|
2
|
-
class TestProducer extends SuperProducer {
|
|
3
|
-
|
|
4
|
-
// constructor
|
|
5
|
-
constructor(taskname, parentfolder) {
|
|
6
|
-
super(taskname, parentfolder)
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
setTaskTrunkInfo(trunkInfo){
|
|
10
|
-
this.trunkInfo = trunkInfo;
|
|
11
|
-
}
|
|
12
|
-
getAllActions() {
|
|
13
|
-
/**
|
|
14
|
-
* Generate all your tasks in this method, return as an array;
|
|
15
|
-
* These data will be allocated to multiple threads (workers) for running;
|
|
16
|
-
* You can obtain each of the data and process it in the 'processTask' method of your consumer.
|
|
17
|
-
*
|
|
18
|
-
* **/
|
|
19
|
-
|
|
20
|
-
return new Promise((resolve, reject)=>{
|
|
21
|
-
let trunkInfo = this.trunkInfo;
|
|
22
|
-
let alltasks = []
|
|
23
|
-
for(let i=0;i<200;i++){
|
|
24
|
-
let task_props = {
|
|
25
|
-
index: i,
|
|
26
|
-
name: `task-${i}`,
|
|
27
|
-
description: `This prop is for a subtask`
|
|
28
|
-
};
|
|
29
|
-
alltasks.push(task_props);
|
|
30
|
-
}
|
|
31
|
-
resolve(alltasks);//return all your tasks as an array
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
let SuperProducer = require('../../index').SuperProducer;
|
|
2
|
+
class TestProducer extends SuperProducer {
|
|
3
|
+
|
|
4
|
+
// constructor
|
|
5
|
+
constructor(taskname, parentfolder) {
|
|
6
|
+
super(taskname, parentfolder)
|
|
7
|
+
|
|
8
|
+
}
|
|
9
|
+
setTaskTrunkInfo(trunkInfo){
|
|
10
|
+
this.trunkInfo = trunkInfo;
|
|
11
|
+
}
|
|
12
|
+
getAllActions() {
|
|
13
|
+
/**
|
|
14
|
+
* Generate all your tasks in this method, return as an array;
|
|
15
|
+
* These data will be allocated to multiple threads (workers) for running;
|
|
16
|
+
* You can obtain each of the data and process it in the 'processTask' method of your consumer.
|
|
17
|
+
*
|
|
18
|
+
* **/
|
|
19
|
+
|
|
20
|
+
return new Promise((resolve, reject)=>{
|
|
21
|
+
let trunkInfo = this.trunkInfo;
|
|
22
|
+
let alltasks = []
|
|
23
|
+
for(let i=0;i<200;i++){
|
|
24
|
+
let task_props = {
|
|
25
|
+
index: i,
|
|
26
|
+
name: `task-${i}`,
|
|
27
|
+
description: `This prop is for a subtask`
|
|
28
|
+
};
|
|
29
|
+
alltasks.push(task_props);
|
|
30
|
+
}
|
|
31
|
+
resolve(alltasks);//return all your tasks as an array
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
35
|
module.exports = TestProducer;
|
package/examples/example1/run.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
let RunnerProducer = require('./RunnerProducer');//You need to provide a Producer to return all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
2
|
-
let RunnerConsumer = require('./RunnerConsumer');//You need to provide a Consumer to process a certain sub-task and return the processed data
|
|
3
|
-
let master = require('../../index').master;
|
|
4
|
-
|
|
5
|
-
master.start(RunnerProducer, RunnerConsumer, {
|
|
6
|
-
multi_task_parent_folder: `C:/tmp-demo`,
|
|
7
|
-
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
8
|
-
numberOfWorkers: 6
|
|
1
|
+
let RunnerProducer = require('./RunnerProducer');//You need to provide a Producer to return all subtasks, multi-tasks will automatically split them and execute them in a multi-threaded manner
|
|
2
|
+
let RunnerConsumer = require('./RunnerConsumer');//You need to provide a Consumer to process a certain sub-task and return the processed data
|
|
3
|
+
let master = require('../../index').master;
|
|
4
|
+
|
|
5
|
+
master.start(RunnerProducer, RunnerConsumer, {
|
|
6
|
+
multi_task_parent_folder: `C:/tmp-demo`,
|
|
7
|
+
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
8
|
+
numberOfWorkers: 6
|
|
9
9
|
});
|
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
let SuperConsumer = require('../../index').SuperConsumer;
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
let _do = (mainTaskInfo, actions, callback)=>{
|
|
5
|
-
if(actions.length === 0) return callback({
|
|
6
|
-
data:'subtask finished, random data='+Math.random()
|
|
7
|
-
});
|
|
8
|
-
let actionData = actions.shift();
|
|
9
|
-
|
|
10
|
-
//start to write your process code here
|
|
11
|
-
|
|
12
|
-
console.log('actionData', actionData)
|
|
13
|
-
|
|
14
|
-
//end of your process code.
|
|
15
|
-
|
|
16
|
-
//trigger next task
|
|
17
|
-
_do(mainTaskInfo, actions, callback);
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
class TestConsumer extends SuperConsumer {
|
|
22
|
-
|
|
23
|
-
// constructor
|
|
24
|
-
constructor(taskname, parentfolder) {
|
|
25
|
-
super(taskname, parentfolder)
|
|
26
|
-
|
|
27
|
-
}
|
|
28
|
-
setTaskTrunkInfo(trunkInfo){
|
|
29
|
-
this.trunkInfo = trunkInfo;
|
|
30
|
-
}
|
|
31
|
-
run (mainTaskInfo, subTaskInfo, taskInfo, callback){
|
|
32
|
-
let actions = subTaskInfo.actions;
|
|
33
|
-
mainTaskInfo.defination = this.trunkInfo.defination;
|
|
34
|
-
_do(mainTaskInfo, actions, (result)=>{
|
|
35
|
-
callback(result)
|
|
36
|
-
})
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
}
|
|
1
|
+
let SuperConsumer = require('../../index').SuperConsumer;
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
let _do = (mainTaskInfo, actions, callback)=>{
|
|
5
|
+
if(actions.length === 0) return callback({
|
|
6
|
+
data:'subtask finished, random data='+Math.random()
|
|
7
|
+
});
|
|
8
|
+
let actionData = actions.shift();
|
|
9
|
+
|
|
10
|
+
//start to write your process code here
|
|
11
|
+
|
|
12
|
+
console.log('actionData', actionData)
|
|
13
|
+
|
|
14
|
+
//end of your process code.
|
|
15
|
+
|
|
16
|
+
//trigger next task
|
|
17
|
+
_do(mainTaskInfo, actions, callback);
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class TestConsumer extends SuperConsumer {
|
|
22
|
+
|
|
23
|
+
// constructor
|
|
24
|
+
constructor(taskname, parentfolder) {
|
|
25
|
+
super(taskname, parentfolder)
|
|
26
|
+
|
|
27
|
+
}
|
|
28
|
+
setTaskTrunkInfo(trunkInfo){
|
|
29
|
+
this.trunkInfo = trunkInfo;
|
|
30
|
+
}
|
|
31
|
+
run (mainTaskInfo, subTaskInfo, taskInfo, callback){
|
|
32
|
+
let actions = subTaskInfo.actions;
|
|
33
|
+
mainTaskInfo.defination = this.trunkInfo.defination;
|
|
34
|
+
_do(mainTaskInfo, actions, (result)=>{
|
|
35
|
+
callback(result)
|
|
36
|
+
})
|
|
37
|
+
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
40
|
module.exports = TestConsumer;
|
|
@@ -1,35 +1,35 @@
|
|
|
1
|
-
let SuperProducer = require('../../index').SuperProducer;
|
|
2
|
-
class TestProducer extends SuperProducer {
|
|
3
|
-
|
|
4
|
-
// constructor
|
|
5
|
-
constructor(taskname, parentfolder) {
|
|
6
|
-
super(taskname, parentfolder)
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
setTaskTrunkInfo(trunkInfo){
|
|
10
|
-
this.trunkInfo = trunkInfo;
|
|
11
|
-
}
|
|
12
|
-
getAllActions() {
|
|
13
|
-
/**
|
|
14
|
-
* Generate all your tasks in this method, return as an array;
|
|
15
|
-
* These data will be allocated to multiple threads (workers) for running;
|
|
16
|
-
* You can obtain each of the data and process it in the 'processTask' method of your consumer.
|
|
17
|
-
*
|
|
18
|
-
* **/
|
|
19
|
-
|
|
20
|
-
return new Promise((resolve, reject)=>{
|
|
21
|
-
let trunkInfo = this.trunkInfo;
|
|
22
|
-
let alltasks = []
|
|
23
|
-
for(let i=0;i<200;i++){
|
|
24
|
-
let task_props = {
|
|
25
|
-
index: i,
|
|
26
|
-
name: `task-${i}`,
|
|
27
|
-
description: `This prop is for a subtask`
|
|
28
|
-
};
|
|
29
|
-
alltasks.push(task_props);
|
|
30
|
-
}
|
|
31
|
-
resolve(alltasks);//return all your tasks as an array
|
|
32
|
-
})
|
|
33
|
-
}
|
|
34
|
-
}
|
|
1
|
+
let SuperProducer = require('../../index').SuperProducer;
|
|
2
|
+
class TestProducer extends SuperProducer {
|
|
3
|
+
|
|
4
|
+
// constructor
|
|
5
|
+
constructor(taskname, parentfolder) {
|
|
6
|
+
super(taskname, parentfolder)
|
|
7
|
+
|
|
8
|
+
}
|
|
9
|
+
setTaskTrunkInfo(trunkInfo){
|
|
10
|
+
this.trunkInfo = trunkInfo;
|
|
11
|
+
}
|
|
12
|
+
getAllActions() {
|
|
13
|
+
/**
|
|
14
|
+
* Generate all your tasks in this method, return as an array;
|
|
15
|
+
* These data will be allocated to multiple threads (workers) for running;
|
|
16
|
+
* You can obtain each of the data and process it in the 'processTask' method of your consumer.
|
|
17
|
+
*
|
|
18
|
+
* **/
|
|
19
|
+
|
|
20
|
+
return new Promise((resolve, reject)=>{
|
|
21
|
+
let trunkInfo = this.trunkInfo;
|
|
22
|
+
let alltasks = []
|
|
23
|
+
for(let i=0;i<200;i++){
|
|
24
|
+
let task_props = {
|
|
25
|
+
index: i,
|
|
26
|
+
name: `task-${i}`,
|
|
27
|
+
description: `This prop is for a subtask`
|
|
28
|
+
};
|
|
29
|
+
alltasks.push(task_props);
|
|
30
|
+
}
|
|
31
|
+
resolve(alltasks);//return all your tasks as an array
|
|
32
|
+
})
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
35
|
module.exports = TestProducer;
|
package/examples/example2/run.js
CHANGED
|
@@ -1,23 +1,23 @@
|
|
|
1
|
-
let RunnerProducer = require('./RunnerProducer');//switch to require from node_modules in your project
|
|
2
|
-
let RunnerConsumer = require('./RunnerConsumer');//switch to require from node_modules in your project
|
|
3
|
-
|
|
4
|
-
let taskId = Math.random()+'';
|
|
5
|
-
let multi_task_parent_folder = `C:/tmp-demo`;
|
|
6
|
-
|
|
7
|
-
let producer = new RunnerProducer(`workers_of_${taskId}`, multi_task_parent_folder)
|
|
8
|
-
let consumer = new RunnerConsumer();
|
|
9
|
-
let master = require('../../index').master;
|
|
10
|
-
|
|
11
|
-
console.log(producer.getTaskName())
|
|
12
|
-
//return;
|
|
13
|
-
|
|
14
|
-
let taskConfig = {
|
|
15
|
-
multi_task_parent_folder
|
|
16
|
-
}
|
|
17
|
-
//console.log(resp)
|
|
18
|
-
producer.setTaskTrunkInfo(taskConfig);
|
|
19
|
-
consumer.setTaskTrunkInfo(taskConfig)
|
|
20
|
-
master.run(producer, consumer, {
|
|
21
|
-
quotaOfEachTask:2,
|
|
22
|
-
numberOfWorkers: 6
|
|
1
|
+
let RunnerProducer = require('./RunnerProducer');//switch to require from node_modules in your project
|
|
2
|
+
let RunnerConsumer = require('./RunnerConsumer');//switch to require from node_modules in your project
|
|
3
|
+
|
|
4
|
+
let taskId = Math.random()+'';
|
|
5
|
+
let multi_task_parent_folder = `C:/tmp-demo`;
|
|
6
|
+
|
|
7
|
+
let producer = new RunnerProducer(`workers_of_${taskId}`, multi_task_parent_folder)
|
|
8
|
+
let consumer = new RunnerConsumer();
|
|
9
|
+
let master = require('../../index').master;
|
|
10
|
+
|
|
11
|
+
console.log(producer.getTaskName())
|
|
12
|
+
//return;
|
|
13
|
+
|
|
14
|
+
let taskConfig = {
|
|
15
|
+
multi_task_parent_folder
|
|
16
|
+
}
|
|
17
|
+
//console.log(resp)
|
|
18
|
+
producer.setTaskTrunkInfo(taskConfig);
|
|
19
|
+
consumer.setTaskTrunkInfo(taskConfig)
|
|
20
|
+
master.run(producer, consumer, {
|
|
21
|
+
quotaOfEachTask:2,
|
|
22
|
+
numberOfWorkers: 6
|
|
23
23
|
});
|