Implementação de uma interface para utilização de Thread Pool com PHP

v1.0-beta.5 2021-06-04 17:53 UTC

This package is auto-updated.

Last update: 2024-04-05 00:23:46 UTC


This package is a simple PHP Thread Pool manager to create concurrent tasks easily using a great and simple interface.

It's require PHP 7.4 Thread Safe and PHP Parallel extension.


You can install the required extension on your PHP using PECL:

pecl install paralell

Package Installation:

composer require pedrogaldino/php-thread-pool

Quick Start


Task is the place to put the code that will be executed inside a queue. Basically the code that your need to run in a concurrent way without block any parent code execution.

Let's create our task:


namespace App\Tasks;

use Galdino\Threads\Pool\Task;

class MyTask extends Task {

    public function onExecute()
        print "Task is started" . PHP_EOL;  
        print "Task is finished" . PHP_EOL;  

Now, let's create our Task's Pool and dispatch the task to be processed:

use Galdino\Threads\Pool\Bootstrap;  
use Galdino\Threads\Pool\Pool;
use App\Tasks\MyTask;

$bootstrap = new Bootstrap();  
$pool = new Pool($bootstrap);

$myTask = new MyTask('Task name');


That's it. The pool will auto create a Queue to process this task.


The bootstrap contains files that your application needs to run when your task execute. You can pass to the constructor, all the files needed.

$bootstrap = new Bootstrap([

The bootstrap class will try to require the composer autoload file automatically if exists. If you want to disable this functionality set false to the second parameter:

$bootstrap = new Bootstrap([
], false);

Another way to init your bootstrap:

$bootstrap = Bootstrap::addFiles([  


The pool will auto manager the queues and tasks execution.

You can tell to the pool for auto create how many queues you want.

// auto create 5 queues according to the tasks demand
$pool = new Pool($bootstrap, 5);  


Every queue process 1 task at time.

You can manually add new queues to the pool:

$pool->addQueue('My queue name');

If you don't need the pool, you can create a queue separately too

use Galdino\Threads\Pool\Bootstrap;
use Galdino\Threads\Pool\Queue;

$bootstrap = new Bootstrap();
$queue = new Queue('My queue name', $bootstrap);


Remember, Tasks don't share memory between the parent Thread. So it's important to always load any bootstrap that your application needs.