-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenericExecutorService.php
37 lines (33 loc) · 1.06 KB
/
GenericExecutorService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
class GenericExecutorService {
private array $workers = [];
private int $maxWorkers;
public function __construct(int $maxWorkers = 5) {
$this->maxWorkers = $maxWorkers;
}
public function submit(callable $task) {
if (count($this->workers) < $this->maxWorkers) {
$pid = pcntl_fork();
if ($pid == -1) {
// Fork failed
die('Could not fork process');
} elseif ($pid) {
// Parent process
$this->workers[$pid] = $task;
} else {
// Child process
$task();
exit(0); // End the child process after task execution
}
} else {
echo "Max workers reached, unable to submit new task.\n";
}
}
public function shutdown() {
foreach ($this->workers as $pid => $task) {
pcntl_waitpid($pid, $status); // Wait for the child process to finish
unset($this->workers[$pid]); // Remove from worker pool
}
}
}
?>