Conditions | 4 |
Paths | 4 |
Total Lines | 22 |
Code Lines | 12 |
Lines | 0 |
Ratio | 0 % |
Tests | 0 |
CRAP Score | 20 |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php |
||
8 | public static function fork(int $numberOfWorkers, callable $asyncServer, int $numberOfFork = 0): void |
||
9 | { |
||
10 | $pid = pcntl_fork(); |
||
11 | if (-1 === $pid) { |
||
12 | // @fail |
||
13 | die('Fork failed'); |
||
|
|||
14 | } |
||
15 | |||
16 | if (0 === $pid) { |
||
17 | $asyncServer(); |
||
18 | pcntl_waitpid($pid, $status); |
||
19 | return; |
||
20 | } |
||
21 | |||
22 | // @parent |
||
23 | $numberOfWorkers--; |
||
24 | ++$numberOfFork; |
||
25 | if ($numberOfWorkers > 0) { |
||
26 | self::fork($numberOfWorkers, $asyncServer, $numberOfFork); |
||
27 | } |
||
28 | |||
29 | pcntl_waitpid($pid, $status); |
||
30 | } |
||
32 |
In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.