-
-
Notifications
You must be signed in to change notification settings - Fork 61
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fork alternative on 0.2? #33
Comments
Multiple processes still work. It just spawns child processes. Why is pcntl important to you? |
You can use the new Prior you might have had code like this: $fork = Fork::spawn(function () {
// $this bound to an instance of Channel.
// Do blocking things.
return $result;
});
$result = yield $fork->join(); In 0.2.x you write the code for the child process in a separate PHP file. <?php // child.php
// You don't have to use $argc or $argv, just showing they're available.
return function (Channel $channel) use ($argc, $argv) {
// Use the instance of Channel provided as first argument.
// Do blocking things.
return $result;
}
// parent.php
$process = Process::run(__DIR__ . "/child.php");
$result = yield $process->join(); I would also have a look at the Also check out the examples directory, particularly the process.php example that runs the code in blocking-process.php. You'll also find some simple examples using workers in that directory. @soullivaneuh If you have further usage questions don't hesitate to ask, especially since I haven't written any formal docs for this library yet. |
BTW @trowski, here is my full usage: $serviceResult = null;
Loop::run(function () use ($containerId, &$serviceResult, $sizeProcess, $currentSize) {
$waitContext = Fork::spawn(function (ContainerManager $containerManager, string $containerId) {
$waitResult = $containerManager->wait($containerId);
return $waitResult->getStatusCode();
}, $this->docker->getContainerManager(), $containerId);
$timer = Loop::repeat(
1000,
function () use ($waitContext, &$serviceResult, $containerId, $sizeProcess, $currentSize): void {
if ((int) $sizeProcess->clearOutput()->mustRun()->getOutput() - $currentSize
> static::MAX_SPACE_USAGE) {
$this->docker->getContainerManager()->kill($containerId);
$waitContext->kill();
$serviceResult = 'Suspicious huge file writing detected.';
Loop::stop();
}
}
);
$delayer = Loop::delay(
static::SERVICE_TIMEOUT * 1000,
function () use ($waitContext, &$serviceResult, $containerId): void {
$this->docker->getContainerManager()->kill($containerId);
$waitContext->kill();
$serviceResult = 'Timeout of '.static::SERVICE_TIMEOUT.' seconds reached.';
Loop::stop();
}
);
try {
$serviceResult = yield $waitContext->join();
} finally {
$waitContext->kill();
Loop::cancel($timer);
Loop::cancel($delayer);
}
}); In a nutshell, I run a docker api command under a child process to be able to look at the execution time and disk space usage. Will your example fit with my need according to you? |
@soullivaneuh pthreads isn't the only alternative to forking, the default was and still is multiple processes if pthreads isn't available, see: parallel/lib/Worker/DefaultWorkerFactory.php Lines 42 to 52 in 0d40e6d
You don't need pthreads for |
@trowski This is wrong for my case, my code is under an anonymous function as described in #33 (comment) @kelunik Thanks for the help but it's hard to understand. What change should I do on my current code to make this working? |
Something like the following should work. If Parent $serviceResult = null;
Loop::run(function () use ($containerId, &$serviceResult, $sizeProcess, $currentSize) {
$waitContext = Process::run(__DIR__ . "/child.php");
$waitContext->send($containerId);
$timer = Loop::repeat(
1000,
function () use ($waitContext, &$serviceResult, $containerId, $sizeProcess, $currentSize): void {
if ((int) $sizeProcess->clearOutput()->mustRun()->getOutput() - $currentSize
> static::MAX_SPACE_USAGE) {
$this->docker->getContainerManager()->kill($containerId);
$waitContext->kill();
$serviceResult = 'Suspicious huge file writing detected.';
Loop::stop();
}
}
);
$delayer = Loop::delay(
static::SERVICE_TIMEOUT * 1000,
function () use ($waitContext, &$serviceResult, $containerId): void {
$this->docker->getContainerManager()->kill($containerId);
$waitContext->kill();
$serviceResult = 'Timeout of '.static::SERVICE_TIMEOUT.' seconds reached.';
Loop::stop();
}
);
try {
$serviceResult = yield $waitContext->join();
} finally {
$waitContext->kill();
Loop::cancel($timer);
Loop::cancel($delayer);
}
}); Child return function (Amp\Parallel\Sync\Channel $channel) {
// Create $containerManager here
$containerId = yield $channel->receive();
$waitResult = $containerManager->wait($containerId);
return $waitResult->getStatusCode();
}; |
@kelunik This might work. But what if the container manager throws an exception on the child process? Will it be thrown to the parent? |
It won't throw the same exception, but throw a |
I finally found a way withou parallel: Loop::run(function () use ($containerId, &$serviceResult, $sizeProcess, $currentSize): void {
$delayer = Loop::delay(
static::SERVICE_TIMEOUT * 1000,
function () use (&$serviceResult, $containerId): void {
$this->docker->getContainerManager()->kill($containerId);
$serviceResult = 'Timeout of '.static::SERVICE_TIMEOUT.' seconds reached.';
Loop::stop();
}
);
Loop::repeat(
1000,
function ($timer) use (&$serviceResult, $containerId, $sizeProcess, $currentSize, $delayer): void {
$running = $this->docker->getContainerManager()->find($containerId)->getState()->getRunning();
$this->logger->info('Docker running state update', [
'running' => $running,
]);
if (!$running) {
$serviceResult = $this->docker->getContainerManager()->find($containerId)->getState()->getExitCode();
Loop::cancel($timer);
Loop::cancel($delayer);
Loop::stop();
}
if ((int) $sizeProcess->clearOutput()->mustRun()->getOutput() - $currentSize
> static::MAX_SPACE_USAGE) {
$this->docker->getContainerManager()->kill($containerId);
$serviceResult = 'Suspicious huge file writing detected.';
Loop::cancel($timer);
Loop::cancel($delayer);
Loop::stop();
}
}
);
}); But I'm note sure of my loop usage. What do you think? |
What worries you? |
I'm going to close this, as the question seems to be answered. If you have further questions or input, please respond so we can reopen or open new issues for unrelated questions. |
@kelunik Sorry for the long delay. I finally managed my need without the loop and parallel system. |
Since 0.2:
What is the alternative to that if I want to keep using pcntl? I can't install pthread.
Regards.
The text was updated successfully, but these errors were encountered: