Skip to content

Commit b96a4bb

Browse files
committed
Fixes with signals. Fast increased
1 parent 9c7da6c commit b96a4bb

File tree

1 file changed

+20
-19
lines changed

1 file changed

+20
-19
lines changed

src/Pool.php

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Pool{
2121
public function __construct(?int $max_childs = null)
2222
{
2323
if(!extension_loaded("pcntl")){
24-
throw new \Error("PCNTL Extension is missing in your PHP build");
24+
throw new MissingExtensionException("PCNTL Extension is missing in your PHP build");
2525
}
2626
$this->pid = getmypid();
2727
$max_childs ??= (self::getCoresCount() ?? 1) * 50;
@@ -34,18 +34,21 @@ public function __construct(?int $max_childs = null)
3434
public function checkChilds()
3535
{
3636
self::breakpoint("checkChilds()");
37-
$removed = false;
37+
$removed = 0;
3838
foreach ($this->childs as $key => $child) {
3939
if(!self::isProcessRunning($child)){
4040
unset($this->childs[$key]);
41-
self::breakpoint("Removed child n. $key");
42-
$removed = true;
41+
$removed++;
4342
}
4443
}
45-
if(!$removed){
44+
if($removed === 0){
4645
self::breakpoint("CheckChilds didn't remove any child");
46+
return false;
4747
}
48-
return $removed;
48+
else{
49+
self::breakpoint("CheckChilds removed $removed childs");
50+
return true;
51+
};
4952
}
5053

5154
public function enqueue(Closure $closure, array $args): void
@@ -60,7 +63,7 @@ protected function _parallel(Closure $closure, ...$args)
6063
self::breakpoint("parallel can be done: current childs: ".count($this->childs)."/".$this->max_childs);
6164
$pid = pcntl_fork();
6265
if ($pid == -1) {
63-
die('could not fork');
66+
throw new CouldNotForkException("Pool could not fork");
6467
}
6568
elseif($pid){
6669
// we are the parent
@@ -71,6 +74,7 @@ protected function _parallel(Closure $closure, ...$args)
7174
else{
7275
// we are the child
7376
$this->is_parent = false;
77+
pcntl_signal(SIGINT, SIG_IGN);
7478
$closure($args);
7579
exit;
7680
}
@@ -92,7 +96,7 @@ public function parallel(Closure $closure, ...$args)
9296
return $this->enqueue($closure, $args);
9397
}
9498
}
95-
elseif(count($this->childs) > 1){
99+
elseif(count($this->childs) > $this->max_childs/2){
96100
$this->checkChilds();
97101
}
98102
return $this->_parallel($closure, ...$args);
@@ -136,16 +140,7 @@ public function __destruct()
136140
if($this->is_parent){
137141
$this->need_tick = false;
138142
self::breakpoint("triggered destructor");
139-
while($this->hasQueue()){
140-
self::breakpoint("queue is not empty");
141-
$this->resolveQueue();
142-
usleep(10000);
143-
}
144-
while($this->hasChilds()){
145-
self::breakpoint("there are still childs");
146-
$this->checkChilds();
147-
usleep(10000);
148-
}
143+
$this->wait();
149144
}
150145
}
151146

@@ -181,7 +176,7 @@ public static function getCoresCount(): ?int
181176

182177
public static function breakpoint($value){
183178
return;
184-
usleep(20000);
179+
usleep(5000);
185180
print($value.PHP_EOL);
186181
}
187182

@@ -226,6 +221,12 @@ public function waitChilds(): void
226221
usleep(10000);
227222
}
228223
}
224+
225+
public function wait(): void
226+
{
227+
$this->waitQueue();
228+
$this->waitChilds();
229+
}
229230
}
230231

231232

0 commit comments

Comments
 (0)