Skip to content

Commit

Permalink
doc-block corrections, simplify parallel closure setup, remove no l…
Browse files Browse the repository at this point in the history
…onger needed function
  • Loading branch information
TheTechsTech committed May 1, 2021
1 parent 41f4c2c commit ff579c9
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 123 deletions.
4 changes: 1 addition & 3 deletions Spawn/Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
131 changes: 47 additions & 84 deletions Spawn/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -383,20 +379,56 @@ 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

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.
*
Expand All @@ -410,23 +442,23 @@ 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)
{
return $displayOutput ? $future->displayOn()->run() : $future->run();
}

/**
* return the full output of the process.
* return the full output of the `Future` process.
*/
function spawn_output(FutureInterface $future)
{
return $future->getOutput();
}

/**
* return the result of the process.
* return the result of the `Future` process.
*/
function spawn_result(FutureInterface $future)
{
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
Loading

0 comments on commit ff579c9

Please sign in to comment.