multi-tasks 1.2.1 → 1.2.3
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 +33 -9
- package/examples/example0/run.js +16 -10
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -12,16 +12,38 @@ How to use:
|
|
|
12
12
|
```javascript
|
|
13
13
|
//see examples/example0
|
|
14
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
|
+
|
|
15
41
|
multiTasks({
|
|
16
|
-
tasks:
|
|
17
|
-
processTask
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
},
|
|
22
|
-
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files
|
|
23
|
-
quotaOfEachTask:2, //The number of tasks assigned to each worker
|
|
24
|
-
numberOfWorks: 6
|
|
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
|
+
numberOfWorks: 6 //Assign how many workers are working in parallel
|
|
25
47
|
});
|
|
26
48
|
|
|
27
49
|
```
|
|
@@ -49,6 +71,8 @@ node examples/example1/run
|
|
|
49
71
|
|
|
50
72
|
Changelog:
|
|
51
73
|
|
|
74
|
+
- 1.2.3 Update README
|
|
75
|
+
- 1.2.2 Update README and examples
|
|
52
76
|
- 1.2.1 Handle exceptions and errors in subtasks
|
|
53
77
|
- 1.2.0 Simplified usage by providing the function way and support return Promise
|
|
54
78
|
- 1.1.4 Remove make-dir
|
package/examples/example0/run.js
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
let multiTasks = require('../../index').multiTasks;
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
|
|
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++){
|
|
5
6
|
let task_props = {
|
|
6
7
|
index: i,
|
|
7
8
|
name: `task-${i}`,
|
|
@@ -10,20 +11,25 @@ for(let i=0;i<12;i++){
|
|
|
10
11
|
alltasks.push(task_props);
|
|
11
12
|
}
|
|
12
13
|
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
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
|
|
16
20
|
if(index===3) throw 'exception';
|
|
17
21
|
if(index===4) return Promise.reject({err:'a test error'});
|
|
22
|
+
|
|
23
|
+
//the returned data will be saved
|
|
18
24
|
return Promise.resolve({
|
|
19
25
|
message: 'this is a result from a demo, random data=' + Math.random()
|
|
20
26
|
});
|
|
21
27
|
}
|
|
22
28
|
|
|
23
29
|
multiTasks({
|
|
24
|
-
tasks: alltasks,
|
|
25
|
-
processTask,
|
|
26
|
-
multi_task_parent_folder: `C:/tmp-demo`, //a directory to store progress and results files
|
|
27
|
-
quotaOfEachTask:
|
|
28
|
-
numberOfWorks: 6
|
|
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
|
+
numberOfWorks: 6 //Assign how many workers are working in parallel
|
|
29
35
|
});
|