22
33namespace skrtdev \async ;
44
5+ use Throwable ;
56
67class Pool{
78
@@ -18,11 +19,17 @@ class Pool{
1819
1920 protected static self $ default_pool ;
2021
22+ /**
23+ * @throws MissingExtensionException
24+ */
2125 public function __construct (?int $ max_childs = null , bool $ kill_childs = true )
2226 {
2327 if (!extension_loaded ('pcntl ' )){
2428 throw new MissingExtensionException ('PCNTL Extension is missing in your PHP build ' );
2529 }
30+ if (!extension_loaded ('posix ' )){
31+ throw new MissingExtensionException ('POSIX Extension is missing in your PHP build ' );
32+ }
2633 $ this ->pid = getmypid ();
2734 $ this ->max_childs = $ max_childs ?? (self ::getCoresCount () ?? 1 ) * 10 ;
2835 $ this ->kill_childs = $ kill_childs ;
@@ -31,7 +38,7 @@ public function __construct(?int $max_childs = null, bool $kill_childs = true)
3138
3239 pcntl_async_signals (true );
3340
34- pcntl_signal (SIGCHLD , function ($ signo , $ status ) {
41+ pcntl_signal (SIGCHLD , function () {
3542 while (true ) {
3643 $ pid = pcntl_waitpid (-1 , $ processState , WNOHANG | WUNTRACED );
3744
@@ -67,7 +74,7 @@ protected function checkChilds(): bool
6774 else {
6875 self ::breakpoint ("CheckChilds removed $ removed childs " );
6976 return true ;
70- };
77+ }
7178 }
7279
7380
@@ -76,6 +83,9 @@ public function enqueue(callable $callable, array $args = []): void
7683 $ this ->queue [] = [$ callable , $ args ];
7784 }
7885
86+ /**
87+ * @throws CouldNotForkException
88+ */
7989 protected function _parallel (callable $ callable , array $ args = [])
8090 {
8191 self ::breakpoint ('started a parallel ' );
@@ -96,11 +106,19 @@ protected function _parallel(callable $callable, array $args = [])
96106 if (!$ this ->kill_childs ) {
97107 pcntl_signal (SIGINT , SIG_IGN );
98108 }
99- $ callable (...$ args );
109+ try {
110+ $ callable (...$ args );
111+ }
112+ catch (Throwable $ e ){
113+ echo "Uncaught $ e " ;
114+ }
100115 exit ;
101116 }
102117 }
103118
119+ /**
120+ * @throws CouldNotForkException
121+ */
104122 public function parallel (callable $ callable , ...$ args )
105123 {
106124 if ($ this ->hasQueue ()){
@@ -262,6 +280,3 @@ public static function getDefaultPool(): self
262280 }
263281
264282}
265-
266-
267- ?>
0 commit comments