1
- <?php
2
- declare (strict_types=1 );
3
- #declare(ticks=1);
1
+ <?php declare (strict_types=1 );
4
2
5
3
namespace skrtdev \async ;
6
4
@@ -32,7 +30,7 @@ public function __construct(?int $max_childs = null, bool $kill_childs = true)
32
30
pcntl_signal (SIGCHLD , SIG_IGN ); // ignores the SIGCHLD signal
33
31
}
34
32
35
- public function checkChilds ()
33
+ public function checkChilds (): bool
36
34
{
37
35
self ::breakpoint ("checkChilds() " );
38
36
$ removed = 0 ;
@@ -52,13 +50,12 @@ public function checkChilds()
52
50
};
53
51
}
54
52
55
- public function enqueue (Closure $ closure , array $ args ): void
53
+ public function enqueue (Closure $ closure , string $ process_title = null , array $ args = [] ): void
56
54
{
57
- $ this ->queue [] = fn () => $ closure ($ args );
58
- // TODO enqueue args
55
+ $ this ->queue [] = [$ closure , $ process_title , $ args ];
59
56
}
60
57
61
- protected function _parallel (Closure $ closure , string $ process_title = null , ... $ args )
58
+ protected function _parallel (Closure $ closure , string $ process_title = null , array $ args = [] )
62
59
{
63
60
self ::breakpoint ("started a parallel " );
64
61
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, ...
81
78
if (isset ($ process_title )){
82
79
@cli_set_process_title ($ process_title );
83
80
}
84
- $ closure ($ args );
81
+ $ closure (... $ args );
85
82
exit ;
86
83
}
87
84
}
88
85
89
86
public function parallel (Closure $ closure , string $ process_title = null , ...$ args )
90
87
{
91
- if (!empty ($ this ->queue )){
88
+ if (count ($ this ->childs ) > $ this ->max_childs /2 ){
89
+ $ this ->checkChilds ();
90
+ }
91
+ if ($ this ->hasQueue ()){
92
92
self ::breakpoint ("resolving queue before parallel() " );
93
- if (!$ this ->resolveQueue ()){
93
+ $ this ->resolveQueue ();
94
+ if ($ this ->hasQueue ()){
94
95
self ::breakpoint ("enqueueing because there is a queue " );
95
- return $ this ->enqueue ($ closure , $ args );
96
+ return $ this ->enqueue ($ closure , $ process_title , $ args );
96
97
}
97
- return false ;
98
98
}
99
99
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 );
107
102
}
108
- return $ this ->_parallel ($ closure , $ process_title , ... $ args );
103
+ return $ this ->_parallel ($ closure , $ process_title , $ args );
109
104
}
110
105
111
- public function resolveQueue ()
106
+ public function resolveQueue (): void
112
107
{
113
108
if ($ this ->is_resolving_queue ) return ;
114
- $ this ->is_resolving_queue = true ;
115
109
116
110
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
+ }
120
116
}
121
117
118
+ $ this ->is_resolving_queue = true ;
119
+
122
120
foreach ($ this ->queue as $ key => $ closure ) {
123
121
if (count ($ this ->childs ) < $ this ->max_childs ){
124
122
unset($ this ->queue [$ key ]);
125
123
self ::breakpoint ("resolveQueue() is resolving n. $ key " );
126
- if ( $ this ->_parallel ($ closure )) break ;
124
+ $ this ->_parallel (... $ closure );
127
125
}
128
126
else {
129
127
self ::breakpoint ("resolveQueue() can't resolve, too many childs " );
@@ -137,7 +135,6 @@ public function resolveQueue()
137
135
}
138
136
139
137
$ this ->is_resolving_queue = false ;
140
- return empty ($ this ->queue );
141
138
142
139
}
143
140
0 commit comments