multi-tasks 1.2.3 → 1.2.5
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/README.md
CHANGED
|
@@ -43,7 +43,7 @@ multiTasks({
|
|
|
43
43
|
processTask,
|
|
44
44
|
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
45
45
|
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
46
|
-
|
|
46
|
+
numberOfWorkers: 6 //Assign how many workers are working in parallel, default is the number of cpu cores minus one
|
|
47
47
|
});
|
|
48
48
|
|
|
49
49
|
```
|
|
@@ -58,7 +58,7 @@ let RunnerConsumer;//See examples, please provide a Consumer class to process a
|
|
|
58
58
|
master.start(RunnerProducer, RunnerConsumer, {
|
|
59
59
|
multi_task_parent_folder: `C:/tmp-demo`,
|
|
60
60
|
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
61
|
-
|
|
61
|
+
numberOfWorkers: 6
|
|
62
62
|
});
|
|
63
63
|
```
|
|
64
64
|
|
|
@@ -71,6 +71,8 @@ node examples/example1/run
|
|
|
71
71
|
|
|
72
72
|
Changelog:
|
|
73
73
|
|
|
74
|
+
- 1.2.5 Rename numberOfWorks to numberOfWorkers, the old one are still supported ;-)
|
|
75
|
+
- 1.2.4 Fix: opt.numberOfWorkers not work
|
|
74
76
|
- 1.2.3 Update README
|
|
75
77
|
- 1.2.2 Update README and examples
|
|
76
78
|
- 1.2.1 Handle exceptions and errors in subtasks
|
package/examples/example0/run.js
CHANGED
|
@@ -31,5 +31,5 @@ multiTasks({
|
|
|
31
31
|
processTask,
|
|
32
32
|
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files, you can check the progress here
|
|
33
33
|
quotaOfEachTask:1, //The number of subtasks assigned to each worker at one time, the default is 1
|
|
34
|
-
|
|
34
|
+
numberOfWorkers: 6 //Assign how many workers are working in parallel
|
|
35
35
|
});
|
package/examples/example1/run.js
CHANGED
package/examples/example2/run.js
CHANGED
package/package.json
CHANGED
|
@@ -1,21 +1,22 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "multi-tasks",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.5",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"directories": {
|
|
7
7
|
"example": "example"
|
|
8
8
|
},
|
|
9
9
|
"scripts": {
|
|
10
|
-
"test": "
|
|
10
|
+
"test": "jest"
|
|
11
11
|
},
|
|
12
12
|
"keywords": [
|
|
13
13
|
"multitask",
|
|
14
14
|
"worker",
|
|
15
15
|
"multithread"
|
|
16
16
|
],
|
|
17
|
-
"dependencies": {
|
|
18
|
-
},
|
|
19
17
|
"author": "zhanglei923@gmail.com",
|
|
20
|
-
"license": "MIT"
|
|
18
|
+
"license": "MIT",
|
|
19
|
+
"devDependencies": {
|
|
20
|
+
"jest": "^29.7.0"
|
|
21
|
+
}
|
|
21
22
|
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
let splitter = require('../workers/taskSplitter');
|
|
2
|
+
|
|
3
|
+
let createArray = (len)=>{
|
|
4
|
+
let alltasks = []
|
|
5
|
+
for(let i=0;i<len;i++){
|
|
6
|
+
alltasks.push(i)
|
|
7
|
+
}
|
|
8
|
+
return alltasks;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
test('test taskSplitter', async () => {
|
|
12
|
+
|
|
13
|
+
expect(splitter.split(createArray(3), 5).length).toBe(1);
|
|
14
|
+
expect(splitter.split(createArray(16), 5).length).toBe(4);
|
|
15
|
+
expect(splitter.split(createArray(101), 1).length).toBe(101);
|
|
16
|
+
expect(splitter.split(createArray(101), 33).length).toBe(4);
|
|
17
|
+
});
|
package/workers/master.js
CHANGED
|
@@ -37,11 +37,12 @@ let hasWorker = ()=>{
|
|
|
37
37
|
return workidlist.size !== 0;
|
|
38
38
|
}
|
|
39
39
|
let run = (currentProducer, currentConsumer, opt)=>{
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
};
|
|
40
|
+
let defaultCPUs = numCPUs - 1;
|
|
41
|
+
if(typeof opt === 'undefined') opt = {};
|
|
43
42
|
let masterId = currentProducer.getTaskName();
|
|
44
|
-
if(typeof opt.
|
|
43
|
+
if(typeof opt.quotaOfEachTask === 'undefined') opt.quotaOfEachTask = 1;
|
|
44
|
+
if(opt.numberOfWorks && !opt.numberOfWorkers) opt.numberOfWorkers = opt.numberOfWorks; //Backward compatible with the wrong variable name
|
|
45
|
+
if(typeof opt.numberOfWorkers === 'undefined') opt.numberOfWorkers = defaultCPUs;
|
|
45
46
|
console.log('master1, masterId=', masterId)
|
|
46
47
|
|
|
47
48
|
|
|
@@ -50,7 +51,7 @@ let run = (currentProducer, currentConsumer, opt)=>{
|
|
|
50
51
|
console.log('start', getDateTimeTxt())
|
|
51
52
|
|
|
52
53
|
if (cluster.isMaster) {//init
|
|
53
|
-
let numWorks = opt.
|
|
54
|
+
let numWorks = opt.numberOfWorkers;
|
|
54
55
|
console.log(`numCPUs ${numCPUs}`);
|
|
55
56
|
console.log(`numWorks ${numWorks}`);
|
|
56
57
|
console.log(`Master=${process.pid}`);
|
|
@@ -155,8 +156,7 @@ let start = (ProducerClass, ConsumerClass, taskConfig)=>{
|
|
|
155
156
|
producer.setTaskTrunkInfo(taskConfig);
|
|
156
157
|
consumer.setTaskTrunkInfo(taskConfig);
|
|
157
158
|
run(producer, consumer, {
|
|
158
|
-
|
|
159
|
-
numberOfWorks: 6
|
|
159
|
+
...taskConfig
|
|
160
160
|
});
|
|
161
161
|
}
|
|
162
162
|
module.exports = {
|