diff --git a/src/Asynchronous.php b/src/Asynchronous.php index 3232a1f..28b561e 100644 --- a/src/Asynchronous.php +++ b/src/Asynchronous.php @@ -70,12 +70,18 @@ class Asynchronous */ $instance->isChild = true; $instance->attachShm(); + try { $response = call_user_func_array($function, $parameters); - shm_put_var($instance->shm, $key, $response ?? Promise::RESPONSE_NONE); + if (is_resource($instance->shm)) + shm_put_var($instance->shm, $key, $response ?? Promise::RESPONSE_NONE); + exit(0); + } catch (\Throwable $throwable) { - shm_put_var($instance->shm, $key, Promise::RESPONSE_ERROR); + if (is_resource($instance->shm)) + shm_put_var($instance->shm, $key, Promise::RESPONSE_ERROR); + exit(1); } } @@ -105,14 +111,6 @@ class Asynchronous self::getInstance()->_awaitChildren(); } - /** - * Very, very inappropriate name. - */ - public static function killChildren() - { - self::getInstance()->_killChildren(); - } - /** * */ @@ -165,21 +163,6 @@ class Asynchronous return $this; } - /** - * @return $this - */ - private function _killChildren() - { - /* - * Require the children to terminate - */ - foreach ($this->children as $index => $pid) - if (!posix_kill($pid, SIGKILL)) - posix_kill($pid, SIGTERM); - - return $this; - } - /** * @return $this */ @@ -264,18 +247,14 @@ class Asynchronous $instance->_awaitChildren(); $instance->_removeShmBlock(); }); + } + public function __destruct() + { /* - * The signal handler + * To be sure - add destructor */ - foreach ([SIGINT, SIGTERM] as $signal) - pcntl_signal($signal, function ($signal) use (&$instance) { - if ($instance->isChild) - return; - - $instance->_killChildren(); - $instance->_removeShmBlock(); - }); + self::removeShmBlock(); } } \ No newline at end of file