PHP code example of uncrackable404 / concurrent-console-progress
1. Go to this page and download the library: Download uncrackable404/concurrent-console-progress library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
uncrackable404 / concurrent-console-progress example snippets
use Uncrackable404\ConcurrentConsoleProgress\ProgressState;
use function Uncrackable404\ConcurrentConsoleProgress\concurrent;
$tasks = [];
for ($i = 1; $i <= 50; $i++) {
$tasks[] = ['queue' => 'main', 'steps' => 1, 'id' => $i];
}
concurrent(
queues: ['main' => ['label' => 'Processing', 'total' => count($tasks)]],
tasks: $tasks,
concurrent: 10,
process: function (array $task, ProgressState $state): void {
usleep(100_000);
$state->advance('main', 1);
},
);
use Uncrackable404\ConcurrentConsoleProgress\ConcurrentProgress;
use Uncrackable404\ConcurrentConsoleProgress\ProgressState;
$queues = [
'users' => ['label' => 'Importing Users', 'total' => 2],
'orders' => ['label' => 'Importing Orders', 'total' => 1],
];
$tasks = [
['queue' => 'users', 'steps' => 1, 'data' => ['id' => 1, 'name' => 'John']],
['queue' => 'users', 'steps' => 1, 'data' => ['id' => 2, 'name' => 'Jane']],
['queue' => 'orders', 'steps' => 1, 'data' => ['id' => 101, 'total' => 50]],
];
$progress = new ConcurrentProgress();
$results = $progress->run(
queues: $queues,
tasks: $tasks,
concurrent: 5,
columns: [
['key' => 'status', 'label' => 'LAST ITEM'],
],
footer: [
['key' => 'memory_peak', 'label' => 'MEMORY PEAK'],
],
process: function (array $task, ProgressState $state): array {
$id = $task['data']['id'] ?? 'unknown';
// Per-queue cell (shown in the `status` column).
$state->set('status', "✅ Processed #{$id}", queue: $task['queue']);
// Global footer value — update atomically.
$state->transform(
'memory_peak',
fn (mixed $current): int => max((int) ($current ?? 0), memory_get_peak_usage(true)),
);
// Advance the progress bar by one step for this queue.
$state->advance($task['queue'], 1);
// The return value is free-form and ends up in the $results array.
return ['id' => $id];
}
);
// $results = [['id' => 1], ['id' => 2], ['id' => 101]]
interface ProgressState
{
public function get(string $key, ?string $queue = null): mixed;
public function set(string $key, mixed $value, ?string $queue = null): void;
public function compareAndSet(string $key, mixed $old, mixed $new, ?string $queue = null): bool;
public function transform(string $key, Closure $fn, ?string $queue = null): mixed;
public function advance(string $queue, int $steps = 1): void;
public function increment(string $key, int|float $delta = 1, ?string $queue = null): int|float;
}
use Illuminate\Console\Command;
use Uncrackable404\ConcurrentConsoleProgress\ProgressState;
use function Uncrackable404\ConcurrentConsoleProgress\concurrent;
class ImportUsers extends Command
{
protected $signature = 'users:import';
public function handle(): int
{
$tasks = User::cursor()->map(fn ($user) => [
'queue' => 'users',
'steps' => 1,
'id' => $user->id,
])->all();
concurrent(
queues: ['users' => ['label' => 'Users', 'total' => count($tasks)]],
tasks: $tasks,
concurrent: 8,
process: function (array $task, ProgressState $state): void {
// your work here
$state->advance('users', 1);
},
);
return self::SUCCESS;
}
}
use Uncrackable404\ConcurrentConsoleProgress\ProgressState;
use function Uncrackable404\ConcurrentConsoleProgress\concurrent;
concurrent(
queues: ['main' => ['label' => 'Processing', 'total' => 1]],
tasks: [['queue' => 'main', 'steps' => 1]],
concurrent: 0,
process: function (array $task, ProgressState $state): void {
dd($task); // works — same process, no fork
$state->advance('main', 1);
},
);