parallel-php / parallel-task
PHP parallel task multi-thread message based library
Installs: 244
Dependents: 0
Suggesters: 0
Security: 0
Stars: 5
Watchers: 2
Forks: 1
Open Issues: 4
pkg:composer/parallel-php/parallel-task
Requires
- php: >=5.5.0
- doctrine/instantiator: ~1.0.5
- ramsey/uuid: ~3.5.1
Requires (Dev)
- drealecs/time-benchmark: ~1.0.0
- php-amqplib/php-amqplib: ~2.6.3
- phpunit/phpunit: ^4.8 || ^5.6
- predis/predis: ~1.1.1
- symfony/process: ~3.1.5
Suggests
- ext-redis: Provides redis extension implementation
- php-amqplib/php-amqplib: Provides rabbitmq PHP implementation
- predis/predis: Provides redis PHP implementation
This package is not auto-updated.
Last update: 2025-10-26 02:14:50 UTC
README
Library for running parallel php tasks in a simple and intuitive way across multiple computers.
What can be done with it:
- Delegate tasks in order to improve application response time. For example, instead of sending an email inline, it can be sent asynchronously.
- Delegate tasks whose result are not required to be immediately visible or for which a response is not needed in the current script. The Examples here can range from simple things like logging up to complex application logic that can be processed with delay in order to improve high peak performance.
- Parallel processing. An algorithm that can be parallelized can benefit from this library by starting multiple workers on different machines and aggregate the results after all finished.
How to use it:
- Choose a queue implementation and create an instance of it. Existing implementations: Redis and RabbitMQ. Help with more implementations is appreciated.
- Build the worker using queue and start it in a cli environment. You can start multiple workers.
$workerBuilder = new ExecutorWorkerBuilder() $worker = $workerBuilder->withConsumeQueue($queue)->buildWorker(); $worker->work($type);
- Build the executor using queue
$executorBuilder = new ExecutorWorkerBuilder() $executor = $executorBuilder->withPublishQueue($queue)->buildExecutor();
- Define a task by implementing the
Taskinterface. - Submit task and get results
$futureResult = $executor->submit($type, MyTask::class, [$param1, $param2]); //...some other time consuming tasks $result = $futureResult->getResult();
Technically, the library is splitted into three parts:
-
Queue module. Handles queue implementation details. From outside, it contains two public interfaces
PublishQueueandConsumeQueueand the input/output entities from it's methods:InputMessage,InputMessageIdentifierandOutputMessage.putInputreceives anInputMessageand store it for processing
submitInputreceives anInputMessage, store it for processing and returns andInputMessageIdentifierfor fetching the result.getOutputreceives anInputMessageIdentifierand returns theOutputMessageresult for it. It's blocking until the result is available.runreceives a callable. It should fetch anInputMessage, run to callable with it as an input and stores the receivedOutput.
-
Task module. Uses Queue module adding a layer of
Taskimplementation. From the outside, it has two classes with two interfaces:TaskSchedulerandTaskRunnereach of them used for scheduling a task for asynchronous run and respectively running tasks asynchronously.The interfaces of
TaskScheduleris:executereceives a new Task and the inputs for it. It should be used for methods that don't have a return type.
submitreceives a new Task and the inputs for it and returns aFutureTaskResult. Should be used for methods that returns something.
The interfaces of
TaskRunneris:runruns tasks asynchronously in a loop
runOnceruns only one task asynchronously
-
Executor/Worker facade. It is just a wrapper around Task module to allow easier composition and usage.
There is a builder that helps creating
ExecutororWorkerallowing to configure implementation forPublishQueueandConsumeQueueand to change default implementation ofTaskInputMessageTransformer,TaskFactory,TaskResultMessageTransformerandTaskRunnerSupervisor.