From ff579c9bf9e89fb177d2454663e7b90295a9e54a Mon Sep 17 00:00:00 2001 From: techno-express Date: Sat, 1 May 2021 12:39:08 -0400 Subject: [PATCH] doc-block corrections, simplify `parallel` closure setup, remove no longer needed function --- Spawn/Container.php | 4 +- Spawn/Core.php | 131 ++++++++++++-------------------- Spawn/FutureInterface.php | 66 ++++++++-------- tests/ChanneledFallbackTest.php | 2 +- tests/SpawnTest.php | 4 +- 5 files changed, 84 insertions(+), 123 deletions(-) diff --git a/Spawn/Container.php b/Spawn/Container.php index bb17fac..c0c5460 100644 --- a/Spawn/Container.php +++ b/Spawn/Container.php @@ -34,10 +34,8 @@ $output = $task(\spawn_channel()); \fflush(\STDOUT); - \usleep(25); + \usleep((\IS_WINDOWS ? 1500 : 100)); \fwrite(\STDOUT, \serializer($output)); - \usleep(25); - \fflush(\STDOUT); exit(0); } catch (\Throwable $exception) { $output = new SerializableException($exception); diff --git a/Spawn/Core.php b/Spawn/Core.php index dae758b..f5755e1 100644 --- a/Spawn/Core.php +++ b/Spawn/Core.php @@ -338,11 +338,7 @@ function parallel($task, ...$argv): FutureInterface // @codeCoverageIgnoreStart $executable = function () use ($task, $argv, $___parallel___) { - if (\is_array($___parallel___)) - \set_globals($___parallel___); - - global $___channeled___; - $___channeled___ = 'parallel'; + \parallel_setup($___parallel___); return $task(...$argv); }; // @codeCoverageIgnoreEnd @@ -383,13 +379,7 @@ function paralleling(?\Closure $task = null, ?string $include = null, ...$args): if (!empty($include) && \is_string($include)) require $include; - if (\is_array($___parallel___)) - \set_globals($___parallel___); - - - global $___channeled___; - $___channeled___ = 'parallel'; - + \parallel_setup($___parallel___); return $task(...$args); }; // @codeCoverageIgnoreEnd @@ -397,6 +387,48 @@ function paralleling(?\Closure $task = null, ?string $include = null, ...$args): return Spawn::create($executable, 0, $channel, true)->displayOn(); } + /** + * Returns an array of all `user defined` global variables, without `super globals`. + * + * @param array $vars only **get_defined_vars()** should be passed in. + * @return array + */ + function get_globals(array $vars): array + { + $global = @\array_diff($vars, array(array())); + unset($global['argc']); + return $global; + } + + /** + * Returns an array of all `user defined` global variables, without `super globals`. + * @return array + * + * @codeCoverageIgnore + */ + function parallel_globals(): array + { + return \get_globals(get_defined_vars()); + } + + /** + * Setup `user defined` global `key => value` pair to be transferred to `Future` **subprocess**. + * - Also an indicator for a `Channel` that it been started by `subprocess` Future. + * + * @param array|null $keyValue + * @return void + */ + function parallel_setup(?array $keyValue = null): void + { + global $___channeled___; + + if (\is_array($keyValue)) + foreach ($keyValue as $key => $value) + $GLOBALS[$key] = $value; + + $___channeled___ = 'parallel'; + } + /** * Destroy `All` Channel instances. * @@ -410,7 +442,7 @@ function channel_destroy() } /** - * Start the process and wait to terminate, and return any results. + * Start the `Future` process and wait to terminate, and return any results. */ function spawn_run(FutureInterface $future, bool $displayOutput = false) { @@ -418,7 +450,7 @@ function spawn_run(FutureInterface $future, bool $displayOutput = false) } /** - * return the full output of the process. + * return the full output of the `Future` process. */ function spawn_output(FutureInterface $future) { @@ -426,7 +458,7 @@ function spawn_output(FutureInterface $future) } /** - * return the result of the process. + * return the result of the `Future` process. */ function spawn_result(FutureInterface $future) { @@ -473,42 +505,6 @@ function spawn_encode($task): string return Spawn::encodeTask($task); } - /** - * Returns an array of all `user defined` global variables, without `super globals`. - * - * @param array $vars only **get_defined_vars()** should be passed in. - * @return array - */ - function get_globals(array $vars): array - { - $global = @\array_diff($vars, array(array())); - unset($global['argc']); - return $global; - } - - /** - * Returns an array of all `user defined` global variables, without `super globals`. - * @return array - * - * @codeCoverageIgnore - */ - function parallel_globals(): array - { - return \get_globals(get_defined_vars()); - } - - /** - * Set `user defined` global `key => value` pair to be transferred to a **subprocess**. - * - * @param array $spawn_globals from `get_globals(get_defined_vars());`. - * @return void - */ - function set_globals(array $spawn_globals): void - { - foreach ($spawn_globals as $key => $value) - $GLOBALS[$key] = $value; - } - /** * Check if a string is base64 valid, or has `encoded` mixed data. * @@ -582,39 +578,6 @@ function deserialize($input) return \is_base64($input) ? \deserializer($input) : $input; } - /** - * For use when/before calling the actual `return` keyword, will flush, then sleep for `microsecond`, - * and return the to be encoded `data/result`. - * - * - For use with subprocess `ipc` interaction. - * - * - This function is intended to overcome an issue when **`return`ing** the `encode` data/results - * from an child subprocess operation. - * - * - The problem is the fact the last output is being mixed in with the `return` encode - * data/results. - * - * - The parent is given no time to read data stream before the `return`, there was no - * delay or processing preformed between child last output and the `return` statement. - * - * @param mixed $with to return to parent process. - * @param int $microsecond - `50` when using `uv_spawn`, otherwise `1500` or so higher with `proc_open`. - * - * @return void|mixed - * - * @codeCoverageIgnore - */ - function flush_value($with = null, int $microsecond = 50) - { - \fflush(\STDOUT); - \usleep($microsecond); - \fflush(\STDOUT); - \usleep($microsecond); - - if (!\is_null($with)) - return $with; - } - /** * Setup for third party integration. * diff --git a/Spawn/FutureInterface.php b/Spawn/FutureInterface.php index 97e3cbb..1151e7d 100644 --- a/Spawn/FutureInterface.php +++ b/Spawn/FutureInterface.php @@ -16,42 +16,42 @@ interface FutureInterface const INVALID = ['Tjs=']; /** - * Gets PHP's process ID. + * Gets PHP's `Future` process ID. * * @return int */ public function getId(): int; /** - * Start the process. + * Start the `Future` process. * * @return FutureInterface */ public function start(): FutureInterface; /** - * Restart the process. + * Restart the `Future` process. * * @return FutureInterface */ public function restart(): FutureInterface; /** - * Start the process and wait to terminate. + * Start the `Future` process and wait to terminate. * * @param bool $useYield - should we use generator callback functions */ public function run(bool $useYield = false); /** - * Return an generator that can start the process and wait to terminate. + * Return an generator that can start the `Future` process and wait to terminate. * * @return \Generator */ public function yielding(); /** - * Close out the process, and reset any related data. + * Close out the `Future` process, and reset any related data. */ public function close(); @@ -64,7 +64,7 @@ public function close(); public function wait($waitTimer = 1000, bool $useYield = false); /** - * Add handlers to be called when the process is successful, erred or progressing in real time. + * Add handlers to be called when the `Future` process is successful, erred or progressing in real time. * * @param callable $doneCallback * @param callable $failCallback @@ -79,7 +79,7 @@ public function then( ): FutureInterface; /** - * Add handlers to be called when the process is terminated with a signal. + * Add handlers to be called when the `Future` process is terminated with a signal. * - This feature is only available when using `libuv`. * * @param int $signal @@ -90,13 +90,13 @@ public function then( public function signal(int $signal, callable $signalCallback): FutureInterface; /** - * Add handlers to be called when the process progressing, it's producing output. + * Add handlers to be called when the `Future` process progressing, it's producing output. * This can be use as a IPC handler for real time interaction. * * The callback will receive **output type** either(`out` or `err`), * and **the output** in real-time. * - * Use: __Channeled__->`send()` to write to the standard input of the process. + * Use: __Channeled__->`send()` to write to the standard input of the `Future` process. * * @param callable $progressCallback * @@ -105,7 +105,7 @@ public function signal(int $signal, callable $signalCallback): FutureInterface; public function progress(callable $progressCallback): FutureInterface; /** - * Add handlers to be called when the process has errors. + * Add handlers to be called when the `Future` process has errors. * * @param callable $callback * @@ -114,7 +114,7 @@ public function progress(callable $progressCallback): FutureInterface; public function catch(callable $callback): FutureInterface; /** - * Add handlers to be called when the process has timed out. + * Add handlers to be called when the `Future` process has timed out. * * @param callable $callback * @@ -132,42 +132,42 @@ public function timeout(callable $callback): FutureInterface; public function clean($output = null); /** - * Return and set the last/final output coming from the child process. + * Return and set the final result/value coming from the child `Future` process. * * @return mixed */ public function getResult(); /** - * Return the last output posted by the child process. + * Return the last output posted by the child `Future` process. * * @return mixed */ public function getLast(); /** - * Returns the current output of the process (STDOUT). + * Returns `All` output of the `Future` process (STDOUT). * - * @return string The process output + * @return string */ public function getOutput(); /** - * Returns the current error output of the process (STDERR). + * Returns `All` error output of the process (STDERR). * - * @return string The process error output + * @return string */ public function getErrorOutput(); /** - * Returns the Pid (process identifier), if applicable. + * Returns the Pid (`Future` process identifier), if applicable. * - * @return int|null The process id if running, null otherwise + * @return int|null */ public function getPid(): ?int; /** - * Stops the running process, with signal. + * Stops the running `Future` process, with signal. * * @param int $signal The signal to send to the process, default is SIGKILL (9) * @@ -176,49 +176,49 @@ public function getPid(): ?int; public function stop(int $signal = \SIGKILL): FutureInterface; /** - * Check if the process has timeout (max. runtime). + * Check if the `Future` process has timeout (max. runtime). * * @return bool */ public function isTimedOut(): bool; /** - * Checks if the process received a signal. + * Checks if the `Future` process received a signal. * * @return bool */ public function isSignaled(): bool; /** - * Checks if the process is currently running. + * Checks if the `Future` process is currently running. * - * @return bool true if the process is currently running, false otherwise + * @return bool true if the `Future` process is currently running, false otherwise */ public function isRunning(): bool; /** - * Checks if the process is terminated. + * Checks if the `Future` process is terminated. * - * @return bool true if process is terminated, false otherwise + * @return bool true if `Future` process is terminated, false otherwise */ public function isTerminated(): bool; /** - * Checks if the process ended successfully. + * Checks if the `Future` process ended successfully. * - * @return bool true if the process ended successfully, false otherwise + * @return bool true if the `Future` process ended successfully, false otherwise */ public function isSuccessful(): bool; /** - * Checks if the process has started. + * Checks if the `Future` process has started. * * @return bool */ public function isStarted(): bool; /** - * Set process to display output of child process. + * Set `Future` process to display output of child process. * * @return FutureInterface */ @@ -226,14 +226,14 @@ public function displayOn(): FutureInterface; /** - * Stop displaying output of child process. + * Stop displaying output of child `Future` process. * * @return FutureInterface */ public function displayOff(): FutureInterface; /** - * The **PHP** process handler, Either `Process` or `UVProcess`. + * The **PHP** `Future` process handler, Either `Process` or `UVProcess`. * * @return Process|\UVProcess */ diff --git a/tests/ChanneledFallbackTest.php b/tests/ChanneledFallbackTest.php index 3dc9acc..79a5b3e 100644 --- a/tests/ChanneledFallbackTest.php +++ b/tests/ChanneledFallbackTest.php @@ -21,7 +21,7 @@ public function testSimpleChanneled() $channel->write('ping'); echo $channel->read(); echo $channel->read(); - return flush_value(9, 1500); + return 9; }, 10, $ipc) ->progress( function ($type, $data) use ($ipc) { diff --git a/tests/SpawnTest.php b/tests/SpawnTest.php index 92ad43f..3541391 100644 --- a/tests/SpawnTest.php +++ b/tests/SpawnTest.php @@ -144,7 +144,7 @@ function () { usleep(1000); echo 'child'; usleep(1000); - return flush_value(3); + return 3; } ); $p->run(); @@ -334,7 +334,7 @@ public function setGlobal() { global $test; $test = 100; - set_globals(['test' => 2, 'other' => 'foo']); + parallel_setup(['test' => 2, 'other' => 'foo']); $this->assertEquals($GLOBALS['test'], 2); $test = 4; $this->assertEquals($GLOBALS['test'], 4);