PHP code example of i07 / php-simple-threads

1. Go to this page and download the library: Download i07/php-simple-threads 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/ */

    

i07 / php-simple-threads example snippets




class myWorker extends \SimpleThreads\Factory\WorkerFactory {

    public function doWork($arguments) {
    
        //do some work here and return the result of the task
        $mywork = $this->myWorkFunction($arguments);
        return $mywork;
    
    }
    
    private function myWorkFunction($arguments) {
        
        //do stuff based on the $arguments
        
        return $result;
        
    }

}

new myWorker($myDescription);



$my_workers = [
    [
        "id" => "Worker1",
        "command" => "myWorker",
        "payload" => "argument-string"
    ],
    [
        "id" => "Worker2",
        "command" => "myWorker",
        "payload" => [
            "option1" => "value1",
            "argument" => "array"
        ]
    ]
];

$myPool = new \SimpleThreads\Pool($my_workers);
$myPool->run();

// SimpleThreads will hold an object with all workers results, the get the results of all workers:
$results = $myPool->getOutput();

var_dump($results);