fghazaleh/multi-thread-manager

Multi-thread manager using Symfony process component

2.0.0 2023-03-24 10:06 UTC

This package is auto-updated.

Last update: 2024-05-24 12:49:21 UTC


README

A Library to handle a multiple Symfony process component, by creating a command which can be handled in asynchronous (threads).

Supported PHP Versions

  • PHP 7.2
  • PHP 8.0

Index

Installation

$ composer require fghazaleh/multi-thread-manager

Usage

Create instance of ThreadManager.

$threadSize = 10;
$threadManager = \FGhazaleh\MultiThreadManager\ThreadManager::create($threadSize);

or

$threadSize = 10;
$threadStartDelay = 1; //milliseconds
$pollInterval = 120; //milliseconds
$threadManager = new \FGhazaleh\MultiThreadManager\ThreadManager(
                        new \FGhazaleh\MultiThreadManager\ThreadSettings(
                            $threadSize, $threadStartDelay, $pollInterval
                        )               
                  );

or

$threadSettings = \FGhazaleh\MultiThreadManager\ThreadSettings::createFromDefault();
$threadManager = new \FGhazaleh\MultiThreadManager\ThreadManager(
                     $threadSettings              
                  );

Add threads

Add shell script command thread.

$threadManager->addThread('php -r "echo 123; exit(0);"');

Add Symfony process thread.

$process = new Symfony\Component\Process\Process('php -r "echo 123; exit(0);"');
$threadManager->addThread($process);

Add thread object.

$threadManager->addThread(
    \FGhazaleh\MultiThreadManager\Thread::createFromCommand(
        'php -r "echo 123; exit(0);"'
    )
);

Add thread with context.

$threadManager->addThread('php -r "echo 123; exit(0);"', ['data' => 'some data']);

Wait for threads

$threadManager->wait();

Terminate threads

$threadManager->terminate();

Register Events/Listeners in Thread Manager

Register event with class listener.

$threadManager->listen(
    \FGhazaleh\MultiThreadManager\Contracts\EventInterface::EVENT_STARTED, 
    new JobStartedListener()
);
$threadManager->listen(
    \FGhazaleh\MultiThreadManager\Contracts\EventInterface::EVENT_FINISHED, 
    new JobFinishedListener()
);
$threadManager->listen(
    \FGhazaleh\MultiThreadManager\Contracts\EventInterface::EVENT_TIMEOUT, 
    new JobTimeoutListener()
);
...
$threadManager->addThread(...)

Register event closure listener function.

$threadManager->listen(
    \FGhazaleh\MultiThreadManager\Contracts\EventInterface::EVENT_STARTED, 
    function (\FGhazaleh\MultiThreadManager\Contracts\ThreadInterface $thread){
        ...
    }
);

Security Vulnerabilities

if you discover a security vulnerability within this boilerplate, please send an email to Franco Ghazaleh at franco.ghazaleh@gmail.com, or create a pull request if possible. All security vulnerabilities will be promptly addressed. Please reference this page to make sure you are up to date.

License

This project is licensed under the MIT License.