Skip to content

Instantly share code, notes, and snippets.

@mortenson
Created June 6, 2018 22:11
Show Gist options
  • Save mortenson/f9e51e5d4028c2c9ad196f8592e8081a to your computer and use it in GitHub Desktop.
Save mortenson/f9e51e5d4028c2c9ad196f8592e8081a to your computer and use it in GitHub Desktop.
Playing around with concurrency using Symfony's process component
<?php
use Symfony\Component\Process\Process;
// The total sleep time of this queue is 22, but it only takes ~10 seconds to
// run as new commands are started as soon as another finishes and the
// $concurrency limit is not met.
$command_queue = [
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 10',
'sleep 1',
'echo "This is an error!" >&2',
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 1',
'sleep 1',
];
$concurrency = 5;
$current_processes = [];
$collected_errors = [];
while ($command_queue || $current_processes) {
$current_processes = array_filter($current_processes, function (Process $current_process) use (&$collected_errors) {
$is_running = $current_process->isRunning();
$command = $current_process->getCommandLine();
if (!$is_running) {
$error_output = $current_process->getErrorOutput();
if ($error_output) {
$collected_errors[] = "Error when running \"{$command}\":\n $error_output";
}
}
return $is_running;
});
if ($command_queue && count($current_processes) < $concurrency) {
$command = array_shift($command_queue);
$process = new Process($command);
$process->start();
$current_processes[] = $process;
}
}
foreach ($collected_errors as $error) {
echo "[ERROR] $error\n";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment