项目作者: jirufik

项目描述 :
jrf-pip (parallel iteration processing)
高级语言: JavaScript
项目地址: git://github.com/jirufik/jrf-pip.git
创建时间: 2020-05-09T07:35:25Z
项目社区:https://github.com/jirufik/jrf-pip

开源协议:

下载


jrf-pip (parallel iteration processing)

jrf-pip - This is a package for parallel iterative processing of large arrays.
Allows parallel processing asynchronously,
or asynchronously in synchronous style. Processing implemented through Promise.all()

Example

  1. const parallelProcessing = require('jrf-pip');
  2. // Send a message to group users
  3. async function sendMesToGroupUsers() {
  4. // Generate groups of 10,000 users.
  5. // 5000 female
  6. // 5000 males
  7. const groupUsers = generateUsers();
  8. // Filter Function
  9. // Skip Male Users
  10. // i.e. send message only to female
  11. const nextValueFn = ({value, index, arrayValues, iteration}) => {
  12. return value.sex === 'male';
  13. };
  14. // Processing Function
  15. // Send a message
  16. const processingFn = ({value, index, arrayValues, iteration}) => {
  17. const user = value;
  18. user.sendMes(iteration);
  19. };
  20. // Callback function. It will work after the completion of sending the message.
  21. const cb = (stackError) => console.log('Finish parallel iteration processing');
  22. // Starting parallel iterative processing
  23. // An array of arrayValues is specified for processing
  24. // filter function nextValueFn specified
  25. // the processing function of the processingFn array element is specified
  26. // a callback function is set that will be executed when parallel processing is completed
  27. // set the number of 1000 parallel processed array values through Promise.all()
  28. await parallelProcessing({
  29. arrayValues: groupUsers,
  30. nextValueFn,
  31. processingFn,
  32. cb,
  33. parallel: 1000
  34. });
  35. console.log('Before finish');
  36. }
  37. // Send a message to the users of the group in a synchronous style
  38. async function sendMesToGroupUsersSyncStyle() {
  39. // Generate groups of 10,000 users.
  40. // 5000 female
  41. // 5000 males
  42. const groupUsers = generateUsers();
  43. // Filter Function
  44. // Skip Male Users
  45. // i.e. send message only to female
  46. const nextValueFn = ({value, index, arrayValues, iteration}) => {
  47. return value.sex === 'male';
  48. };
  49. // Processing Function
  50. // Send a message
  51. const processingFn = ({value, index, arrayValues, iteration}) => {
  52. const user = value;
  53. user.sendMes(iteration);
  54. };
  55. // Starting parallel iterative processing
  56. // An array of arrayValues is specified for processing
  57. // filter function nextValueFn specified
  58. // the processing function of the processingFn array element is specified
  59. // the awaitRes response wait parameter is specified i.e. synchronous execution
  60. // set the number of 2000 parallel values of the array through Promise.all()
  61. // time of cycleTimeout of an asynchronous pause between iterations is set
  62. const stackError = await parallelProcessing({
  63. arrayValues: groupUsers,
  64. nextValueFn,
  65. processingFn,
  66. awaitRes: true,
  67. parallel: 2000,
  68. cycleTimeout: 100
  69. });
  70. console.log('Finish parallel iteration processing in sync style');
  71. }
  72. function generateUsers(count = 10000) {
  73. const groupUsers = [];
  74. const even = n => !(n % 2);
  75. for (let i = 1; i <= count; i++) {
  76. const sex = even(i) ? 'male' : 'female';
  77. groupUsers.push({
  78. id: i,
  79. sex,
  80. sendMes(iteration) {
  81. console.log(`send mes to user id: ${this.id} sex: ${this.sex} iteration: ${iteration}`);
  82. }
  83. });
  84. }
  85. return groupUsers;
  86. }
  87. Promise.resolve()
  88. .then(sendMesToGroupUsers)
  89. .then(sendMesToGroupUsersSyncStyle)
  90. .catch(e => console.error(e));

Parameters

The parallelProcessing function at the input accepts the following parameters:

name type required default description
arrayValues array true An array of values to process
processingFn function true A synchronous or asynchronous function that processes an array element. The input accepts parameters paramsForFunction
nextValueFn function false A synchronous or asynchronous function that performs filtering. If it returns true, then the current element of the array will not be processed. false - the current element of the array will be processed. The input accepts parameters paramsForFunction
parallel number false 1000 Number of elements to be processed in parallel per iteration
awaitRes boolean false false Expect an answer i.e. wait for completion to complete in synchronous style
cb function false A synchronous or asynchronous callback function, will work if awaitRes = false. An error stack will be passed to the function stackError
cycleTimeout number false 200 Asynchronous pause between iterations (milliseconds), for awaitRes = true

paramsForFunction

Parameters passed to the processingFn and nextValueFn function

name type description
value any Array element to process
index number Array element index
arrayValues array Array
iteration number Iteration number

stackError

An error stack returned with awaitRes = true, or passed to a function
callback when awaitRes = false.

Array of objects:

name type description
value any Array element during processing of which an error occurred
index number Array element index
iteration number Iteration number
error object Error