Skip to content
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

Closed
soullivaneuh opened this issue Dec 29, 2017 · 13 comments
Closed

Fork alternative on 0.2? #33

soullivaneuh opened this issue Dec 29, 2017 · 13 comments
Labels

Comments

@soullivaneuh
Copy link

Since 0.2:

The forking context has been completely removed. Forking the entire process state proved to be too error-prone to be useful as a general-purpose parallel execution context.

What is the alternative to that if I want to keep using pcntl? I can't install pthread.

Regards.

@kelunik
Copy link
Member

kelunik commented Dec 30, 2017

Multiple processes still work. It just spawns child processes. Why is pcntl important to you?

@trowski
Copy link
Member

trowski commented Dec 30, 2017

You can use the new Process API instead. It launches a completely separate PHP process, free of any state that may have been forked from the parent.

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 Task and Worker::enqueue() API, as this may fit your needs and make writing your code even simpler.

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.

@soullivaneuh
Copy link
Author

Why is pcntl important to you?

@kelunik Because I can't install pthread on my server, this is the only solution so far.

But I'll try @trowski sample.

@soullivaneuh
Copy link
Author

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?

@kelunik
Copy link
Member

kelunik commented Jan 4, 2018

@soullivaneuh pthreads isn't the only alternative to forking, the default was and still is multiple processes if pthreads isn't available, see:

public function create(): Worker {
if (Thread::supported()) {
return new WorkerThread($this->className);
}
return new WorkerProcess(
$this->className,
[],
\getenv("AMP_PHP_BINARY") ?: (\defined("AMP_PHP_BINARY") ? \AMP_PHP_BINARY : null)
);
}

You don't need pthreads for amphp/parallel.

@soullivaneuh
Copy link
Author

In 0.2.x you write the code for the child process in a separate PHP file.

@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?

@kelunik
Copy link
Member

kelunik commented Jan 4, 2018

Something like the following should work. If $containerManager is serializeable, you can also send it via the channel.

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();
};

@soullivaneuh
Copy link
Author

@kelunik This might work. But what if the container manager throws an exception on the child process? Will it be thrown to the parent?

@kelunik
Copy link
Member

kelunik commented Jan 4, 2018

It won't throw the same exception, but throw a TaskException instead, as exceptions can't be serialized.

@soullivaneuh
Copy link
Author

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?

@kelunik
Copy link
Member

kelunik commented Jan 6, 2018

What worries you?

@kelunik
Copy link
Member

kelunik commented Jan 12, 2018

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 kelunik closed this as completed Jan 12, 2018
@soullivaneuh
Copy link
Author

soullivaneuh commented Jan 15, 2018

@kelunik Sorry for the long delay. I finally managed my need without the loop and parallel system.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

3 participants