steevanb/php-parallel-processes

Call processes in parallel

Installs: 3 322

Dependents: 0

Suggesters: 0

Security: 0

Stars: 1

Watchers: 1

Forks: 0

Open Issues: 12

pkg:composer/steevanb/php-parallel-processes

1.0.1 2024-12-02 09:26 UTC

README

Version PHP Lines Downloads GitHub workflow status Coverage

php-parallel-processes

Execute processes in parallel.

Examples of use: start your environment, CI tools...

Installation

Use official Docker image

You can use the official Docker image to not install anything: steevanb/php-parallel-processes:x.y.z.

Example:

docker \
    run \
        --rm \
        --tty \
        --interactive \
        --volume "$(pwd)":"$(pwd)" \
        --workdir "$(pwd)" \
        steevanb/php-parallel-processes:x.y.z \
        php parallel-processes.php

If your processes use docker

If processes in parallel-processes.php use Docker, you have to add a volume on your Docker socket:

--volume /var/run/docker.sock:/var/run/docker.sock

All official php-parallel-processes images have Docker and Docker compose installed, so you only need to add a volume on the socket.

alpine, bookworm and buster

3 Docker images are provided for each php-parallel-processes version, use the one you want depending on your needs:

  • alpine: smaller version, but could be "too much simple" sometimes
  • buster: middle version, contains almost everything needed
  • bookworm: larger version, should contain what you need

Install as Composer dependency

If you want to add php-parallel-processes directly in your project:

composer require steevanb/php-parallel-processes ^1.0

Create processes configuration

You need to create a configuration for your processes, written in PHP.

Example: bin/start.php

Basic example

<?php

declare(strict_types=1);

use Steevanb\ParallelProcess\{
    Console\Application\ParallelProcessesApplication,
    Process\Process
};
use Symfony\Component\Console\Input\ArgvInput;

# If you use the official Docker image, you should use the Composer global autoload
require $_ENV['COMPOSER_GLOBAL_AUTOLOAD_FILE_NAME'];
# If you use the Composer dependency version, you should use your Composer autoload
require __DIR__ . '/vendor/autoload.php';

(new ParallelProcessesApplication())
    ->addProcess((new Process(['first', 'process']))->setName('First process'))
    ->addProcess((new Process(['second', 'process']))->setName('Second process'))
    ->run(new ArgvInput($argv));

Configurations

Global timeout

You can configure the global timeout with ParallelProcessesApplication::setTimeout().

Default value: null (no timeout).

(new ParallelProcessesApplication())
    // Timeout is in seconds, here we will have a 10s timeout
    ->setTimeout(10)

Refresh interval

You can configure the refresh interval, to scan all processes status and start the next ones, with ParallelProcessesApplication::setRefreshInterval().

Default value: 10000 (10ms)

(new ParallelProcessesApplication())
    // Timeout is in microseconds, here we will have a 500ms timeout
    ->setRefreshInterval(50000)

Maximum processes in parallel

You can configure the maximum number of processes in parallel with ParallelProcessesApplication::setMaximumParallelProcesses().

Default value: null (no maximum)

(new ParallelProcessesApplication())
    // Here we will have maximum 3 processes in parallel
    ->setMaximumParallelProcesses(3)

Theme

php-parallel-processes comes with 2 themes:

  • default
    • Output everything
    • Use verbosity (-v, -vv or -vvv) to add execution time and process outputs
    • Should be used when you need to see live processes status, most of the time ;)
  • summary
    • Output only the start and the end of parallel processes
    • Use verbosity (-v, -vv or -vvv) to add execution time and process outputs
    • Should be used when you don't need to see live processes status, in CI for example

Configure it with ParallelProcessesApplication::setTheme():

use Steevanb\ParallelProcess\Console\Application\Theme\DefaultTheme();

(new ParallelProcessesApplication())
    ->setTheme(new DefaultTheme())

You can also configure it in CLI with --theme (CLI override PHP configuration):

php parallel-processes.php --theme summary

You can create your own theme by implementing ThemeInterface.