1- <?php
2- declare (strict_types=1 );
3- #declare(ticks=1);
1+ <?php declare (strict_types=1 );
42
53namespace skrtdev \async ;
64
@@ -32,7 +30,7 @@ public function __construct(?int $max_childs = null, bool $kill_childs = true)
3230 pcntl_signal (SIGCHLD , SIG_IGN ); // ignores the SIGCHLD signal
3331 }
3432
35- public function checkChilds ()
33+ public function checkChilds (): bool
3634 {
3735 self ::breakpoint ("checkChilds() " );
3836 $ removed = 0 ;
@@ -52,13 +50,12 @@ public function checkChilds()
5250 };
5351 }
5452
55- public function enqueue (Closure $ closure , array $ args ): void
53+ public function enqueue (Closure $ closure , string $ process_title = null , array $ args = [] ): void
5654 {
57- $ this ->queue [] = fn () => $ closure ($ args );
58- // TODO enqueue args
55+ $ this ->queue [] = [$ closure , $ process_title , $ args ];
5956 }
6057
61- protected function _parallel (Closure $ closure , string $ process_title = null , ... $ args )
58+ protected function _parallel (Closure $ closure , string $ process_title = null , array $ args = [] )
6259 {
6360 self ::breakpoint ("started a parallel " );
6461 self ::breakpoint ("parallel can be done: current childs: " .count ($ this ->childs )."/ " .$ this ->max_childs );
@@ -81,49 +78,50 @@ protected function _parallel(Closure $closure, string $process_title = null, ...
8178 if (isset ($ process_title )){
8279 @cli_set_process_title ($ process_title );
8380 }
84- $ closure ($ args );
81+ $ closure (... $ args );
8582 exit ;
8683 }
8784 }
8885
8986 public function parallel (Closure $ closure , string $ process_title = null , ...$ args )
9087 {
91- if (!empty ($ this ->queue )){
88+ if (count ($ this ->childs ) > $ this ->max_childs /2 ){
89+ $ this ->checkChilds ();
90+ }
91+ if ($ this ->hasQueue ()){
9292 self ::breakpoint ("resolving queue before parallel() " );
93- if (!$ this ->resolveQueue ()){
93+ $ this ->resolveQueue ();
94+ if ($ this ->hasQueue ()){
9495 self ::breakpoint ("enqueueing because there is a queue " );
95- return $ this ->enqueue ($ closure , $ args );
96+ return $ this ->enqueue ($ closure , $ process_title , $ args );
9697 }
97- return false ;
9898 }
9999 elseif (count ($ this ->childs ) > $ this ->max_childs ){
100- if (!$ this ->checkChilds ()){
101- self ::breakpoint ("enqueueing because of max reached (tried checkChilds but no results) " );
102- return $ this ->enqueue ($ closure , $ args );
103- }
104- }
105- elseif (count ($ this ->childs ) > $ this ->max_childs /2 ){
106- $ this ->checkChilds ();
100+ self ::breakpoint ("enqueueing because of max reached (tried checkChilds but no results) " );
101+ return $ this ->enqueue ($ closure , $ process_title , $ args );
107102 }
108- return $ this ->_parallel ($ closure , $ process_title , ... $ args );
103+ return $ this ->_parallel ($ closure , $ process_title , $ args );
109104 }
110105
111- public function resolveQueue ()
106+ public function resolveQueue (): void
112107 {
113108 if ($ this ->is_resolving_queue ) return ;
114- $ this ->is_resolving_queue = true ;
115109
116110 if (count ($ this ->childs ) >= $ this ->max_childs ){
117- self ::breakpoint ("resolveQueue() -> too many childs, trying to remove... " );
118- self ::breakpoint ("check childs from resolveQueue() " );
119- $ this ->checkChilds ();
111+ self ::breakpoint ("resolveQueue() -> too many childs, trying to remove... " .PHP_EOL ."check childs from resolveQueue() " );
112+ if (!$ this ->checkChilds ()){
113+ self ::breakpoint ("resolveQueue() exited because of too many childs " );
114+ return ;
115+ }
120116 }
121117
118+ $ this ->is_resolving_queue = true ;
119+
122120 foreach ($ this ->queue as $ key => $ closure ) {
123121 if (count ($ this ->childs ) < $ this ->max_childs ){
124122 unset($ this ->queue [$ key ]);
125123 self ::breakpoint ("resolveQueue() is resolving n. $ key " );
126- if ( $ this ->_parallel ($ closure )) break ;
124+ $ this ->_parallel (... $ closure );
127125 }
128126 else {
129127 self ::breakpoint ("resolveQueue() can't resolve, too many childs " );
@@ -137,7 +135,6 @@ public function resolveQueue()
137135 }
138136
139137 $ this ->is_resolving_queue = false ;
140- return empty ($ this ->queue );
141138
142139 }
143140
0 commit comments