riki137 / multitron
Tool for managing fast both asynchronous and multi-threaded execution of tasks. Focused on performance and pleasant CLI interface.
Installs: 13 073
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Requires
- php: ^8.2
- ext-mbstring: *
- ext-pcntl: *
- psr/container: ^2.0
- psr/container-implementation: *
- riki137/stream-ipc: ^1.0.0-beta6
- symfony/console: ^7.2
Requires (Dev)
- contributte/psr11-container-interface: ^0.6.0
- illuminate/support: ^11.0
- nette/di: ^3.2
- phpstan/phpstan: ^2.1
- phpunit/phpunit: ^11.5
- psr/log: ^3.0
- squizlabs/php_codesniffer: ^3.13
- symfony/dependency-injection: ^7.3
- tracy/tracy: ^2.10
This package is auto-updated.
Last update: 2025-06-24 14:55:50 UTC
README
Multitron is a powerful, high-performance PHP task orchestrator designed to simplify parallel processing, concurrency, and automation in PHP applications. Quickly execute complex tasks asynchronously, maximizing the efficiency and scalability of your workflows.
Ideal for Symfony Console apps, CI/CD pipelines, batch processing, and heavy PHP workloads.
Why Choose Multitron?
- π Efficient Task Dependency Management: Clearly define task dependencies with intuitive declarative syntax.
- β‘ Optimized Parallel Execution: Automatic CPU core detection for peak performance and resource utilization.
- π§© Partitioned Workloads: Easily split large tasks across multiple worker processes.
- π Real-Time Progress & Transparency: Continuous console logging and progress tracking.
- ποΈ Centralized Cache for Tasks: Inter-process data sharing and communication made simple.
- π Symfony Console Ready: Effortlessly integrate Multitron into your existing Symfony-based applications.
- π Open Source & MIT Licensed: Completely free to use, adapt, and distribute.
Installation
Usage
Tasks implement the Multitron\Execution\Task
interface. Here's a minimal example task:
use Multitron\Comms\TaskCommunicator; use Multitron\Execution\Task; final class HelloTask implements Task { public function execute(TaskCommunicator $comm): void { $comm->log('Hello from a worker'); } }
You can register tasks in a command that extends Multitron\Console\AbstractMultitronCommand
:
use Multitron\Console\AbstractMultitronCommand; use Multitron\Orchestrator\TaskOrchestrator; use Multitron\Tree\TaskTreeBuilder; use Multitron\Tree\TaskTreeBuilderFactory; use Symfony\Component\Console\Attribute\AsCommand; #[AsCommand(name: 'app:tasks')] final class MyCommand extends AbstractMultitronCommand { public function __construct(TaskTreeBuilderFactory $factory, TaskOrchestrator $orchestrator) { parent::__construct($factory, $orchestrator); } public function getNodes(TaskTreeBuilder $b): void { $cache = $b->group('cache-clear', [ $b->service(ClearCacheTask::class), $b->service(ClearLogsTask::class), ]); $b->service(OtherCacheClearTask::class, [$cache]); $b->service(MyFirstTask::class); $second = $b->service(MySecondTask::class); $third = $b->service(MyThirdTask::class, [$second]); $b->partitioned(MyPartitionedTask::class, 4, [$third, $cache]); } }
Register the command in your Symfony Console application and run it. Multitron will execute the tasks respecting dependencies and concurrency.
You can control how many tasks run at once via the -c
/--concurrency
option:
php bin/console app:tasks -c 8
The library will spawn up to eight worker processes and keep them busy until all tasks finish.
To limit which tasks run, pass a pattern as the first argument. Wildcards work the same as in fnmatch()
and you may use %
in place of *
for convenience:
php bin/console app:tasks cache-* # run only tasks whose ID or tag matches "cache-*"
You can combine multiple patterns by separating them with commas. The filter applies to both task IDs and tags and is an OR pattern.
php bin/console app:tasks "db-%,cache-%,final-%"
You can also tune how often progress updates are rendered using the -u
/--update-interval
option (in seconds):
php bin/console app:tasks -u 0.5
You may also constrain memory usage with the -m
/--memory-limit
option:
php bin/console app:tasks -m 512M
You can disable colors with --no-colors
and switch off interactive table rendering using --interactive=no
. The default --interactive=detect
automatically falls back to plain output when run in CI.
Central Cache
Within a task you receive a TaskCommunicator
instance that provides simple methods to read and write data shared between tasks:
use Multitron\Comms\TaskCommunicator; final class MyTask implements Task { public function execute(TaskCommunicator $comm): void { $comm->cache->write(['foo' => ['bar' => 'baz']], 2); $baz = $comm->cache->read(['foo' => ['bar']])->await()['foo']['bar']; // baz $comm->cache->write(['stats' => ['hits' => ($values['stats']['hits'] ?? 0) + 1]], 2); } }
Reporting Progress
Tasks can update progress counters that Multitron displays while running. Use
the ProgressClient
provided by the communicator:
final class DownloadTask implements Task { public function execute(TaskCommunicator $comm): void { $comm->progress->setTotal(100); for ($i = 0; $i < 100; $i++) { // ... work $comm->progress->addDone(); } } }
You may also call addOccurrence()
or addWarning()
to report additional
metrics or warnings.
Partitioned Tasks
When a workload can be split into chunks, partitioned tasks run those chunks in parallel. Define a task extending PartitionedTask
and specify the number of partitions in the tree:
use Multitron\Tree\Partition\PartitionedTask; final class BuildReportTask extends PartitionedTask { public function execute(TaskCommunicator $comm): void { $comm->log("processing part {$this->partitionIndex} of {$this->partitionCount}"); } } $builder->partitioned(BuildReportTask::class, 4);
Accessing CLI Options
Options passed on the command line are forwarded to each task. Retrieve them via
TaskCommunicator
:
final class ProcessUsersTask implements Task { public function execute(TaskCommunicator $comm): void { $limit = (int)($comm->getOption('limit') ?? 0); // ... process with the given $limit } }
Call getOptions()
to receive the entire array of options if needed.
Custom Progress Output
Multitron renders progress using a ProgressOutputFactory
. Replace the default table display or combine outputs with ChainProgressOutputFactory
:
use Multitron\Orchestrator\Output\ChainProgressOutputFactory; use Multitron\Orchestrator\Output\TableOutputFactory; $factory = new ChainProgressOutputFactory( new TableOutputFactory(), new JsonOutputFactory(), // your custom factory ); $orchestrator = new TaskOrchestrator($ipc, $container, $execFactory, $factory, $handlerFactory);
Implement the factory to send progress anywhere you like.
Contribute to Multitron!
Your feedback, issues, and contributions are highly encouraged. Open a GitHub issue or start a pull request to help improve PHP concurrency and task management:
License
Multitron is MIT licensed. See the LICENSE file for full details.
SEO Keywords: PHP Task Orchestrator, Parallel Processing PHP, Symfony CLI Automation, Asynchronous PHP Tasks, Multitron PHP, PHP Concurrency, PHP Task Manager, Open-source PHP Library