PHP code example of fghazaleh / multi-thread-manager

1. Go to this page and download the library: Download fghazaleh/multi-thread-manager 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/ */

    

fghazaleh / multi-thread-manager example snippets


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

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

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

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

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

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

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

$threadManager->wait();

$threadManager->terminate();

$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(...)

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