concurrent_promise

Parallel processing module for JavaScript that can specify the number of concurrent executions.

此脚本不应直接安装。它是供其他脚本使用的外部库,要使用该库请加入元指令 // @require https://update.gf.qytechs.cn/scripts/398566/785036/concurrent_promise.js

  1. ( ( exports ) => {
  2. 'use strict';
  3.  
  4. const
  5. version = '1.0.1',
  6. execute = async ( promise_functions, max_concurrent_worker = 10 ) => {
  7. let concurrent_workers = Array( max_concurrent_worker ).fill( null ),
  8. waiting_worker_queue = [],
  9. success_list = [],
  10. failure_list = [],
  11. log_debug = ( header ) => {
  12. if ( ! exports.debug_mode ) {
  13. return;
  14. }
  15. if ( header ) {
  16. console.log( header );
  17. }
  18. console.log( '- concurrent_workers:', concurrent_workers );
  19. console.log( '- waiting_worker_queue:', waiting_worker_queue );
  20. console.log( '- success_list:', success_list );
  21. console.log( '- failure_list:', failure_list );
  22. },
  23. attempt_work_delegation = () => {
  24. log_debug( '*** attempt_work_delegation()' );
  25. if ( waiting_worker_queue.length <= 0 ) {
  26. return;
  27. }
  28. let target_worker_id = -1;
  29. for ( let worker_id = 0; worker_id < concurrent_workers.length; worker_id ++ ) {
  30. if ( ! concurrent_workers[ worker_id ] ) {
  31. target_worker_id = worker_id;
  32. break;
  33. }
  34. }
  35. if ( target_worker_id < 0 ) {
  36. return;
  37. }
  38. let target_worker = concurrent_workers[ target_worker_id ] = waiting_worker_queue.shift(),
  39. target_promise = target_worker.promise = target_worker.promise_function(),
  40. finish = ( is_success, result ) => {
  41. target_worker.is_success = is_success;
  42. target_worker.result = result;
  43. ( is_success ? success_list : failure_list ).push( target_worker );
  44. target_worker.resolve( result );
  45. concurrent_workers[ target_worker_id ] = null;
  46. attempt_work_delegation();
  47. };
  48. target_worker.worker_id = target_worker_id;
  49. target_promise
  50. .then( ( result ) => {
  51. finish( true, result );
  52. } )
  53. .catch( ( result ) => {
  54. finish( false, result );
  55. } );
  56. },
  57. add_worker = ( worker ) => {
  58. waiting_worker_queue.push( worker );
  59. attempt_work_delegation();
  60. },
  61. wrap_promise_function = ( promise_function, call_index ) => {
  62. return new Promise( ( resolve, reject ) => {
  63. add_worker( {
  64. call_index : call_index,
  65. promise_function : promise_function,
  66. resolve : resolve,
  67. reject : reject,
  68. } );
  69. } );
  70. },
  71. promise_results = await Promise.all( promise_functions.map( ( promise_function, call_index ) => wrap_promise_function( promise_function, call_index ) ) );
  72. success_list.sort( ( a, b ) => a.call_index - b.call_index );
  73. failure_list.sort( ( a, b ) => a.call_index - b.call_index );
  74. log_debug( '*** concurrent_executor() : end' );
  75. let result_info = {
  76. promise_results : promise_results,
  77. success_list : success_list,
  78. failure_list : failure_list,
  79. };
  80. if ( exports.debug_mode ) {
  81. Object.assign( result_info, {
  82. waiting_worker_queue : waiting_worker_queue,
  83. concurrent_workers : concurrent_workers,
  84. } );
  85. }
  86. return result_info;
  87. };
  88.  
  89. Object.assign( exports, {
  90. version : version,
  91. execute : execute,
  92. debug_mode : false,
  93. } );
  94.  
  95. } )( ( typeof exports != 'undefined' ) ? exports : ( ( typeof window != 'undefined' ) ? window : this )[ 'concurrent_promise' ] = {} );

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址