Pthreads PHP : execute Foreach Loop in Parallel

10,689

Comments included.

<?php

//Require Core Component
require_once("xyz.php");

$Threads = new StackableArray();

//A Global Storage that will be shared between threads.
$Storage = new Storage();

//Store {count} in Global Storage
$Storage['count'] = 0;


/*
 * For Each Site, spawn a new thread, process the site and store result in Global Storage.
 */
foreach($sites as $site)
{
    $Thread = new SiteProcess($Storage, $site);
    $Thread->start();
    $Threads[$re] = $Thread;
}

//When the threads are complete, join them(destroy the threads)
foreach($Threads as $Thread)
{
    $Thread->join();
}

//A thread class
class SiteProcess extends Threads
{
    private $Storage;
    private $site;

    public function __construct(Storage $Storage, $site)
    {
        $this->Storage = $Storage;
        $this->site = $site;
    }

    public function run()
    {
        require_once("allsite/{$this->site}.php");
        $siteObj = new $this->site;
        $this->Storage[$this->Storage['count']] = $siteObj->getsiteData($query, $cat);
        $this->Storage['count'] = $this->Storage['count'] + 1;
    }
}

class StackableArray extends Stackable { public function run() { } };

class Storage extends Stackable { public function run() { } };
Share:
10,689
Me7888
Author by

Me7888

Updated on June 04, 2022

Comments

  • Me7888
    Me7888 almost 2 years

    How to convert code from core PHP to Pthread code

    My core PHP code :

    require_once 'xyz.php';
    $count="0";
    
    foreach($sites as $site)
    {
    require_once 'allsite/'.$site.'.php';
    $siteObj = new $site;
    $data[$count] = $siteObj->getsiteData($query,$cat);
    $count++;
    }
    

    How can i execute foreach loop code in Pthread manner ( Multi-threading )

    After foreach loop execution complete normal PHP stuff done (Means No Multi-Threading after foreach Loop)

    Note : I already installed and config pthread in PHP

  • Me7888
    Me7888 about 10 years
    @Michael thnx ...... can you tell me what is use of last two stackableArray and Storage class??................and after multithreading how can i use all $this->Storage[$this->Storage['count']] array value in normal PHP
  • Michael Yoo
    Michael Yoo about 10 years
    @JoeWatkins I would've done that in real-world examples but I was simply porting over the original code to be multi-threaded.
  • Michael Yoo
    Michael Yoo about 10 years
    @Melody Those classes are needed to share data between threads. It is not possible to share normal arrays between threads.
  • Joe Watkins
    Joe Watkins about 10 years
    @Melody after the threads are completed the main process can cast the object back to a normal array and manipulate that ... or you can use the builtin ArrayAccess on the object ...
  • Me7888
    Me7888 about 10 years
    @MichaelYoo i am try your above code but i got error : PHP Fatal error: Class 'StackableArray' not found on line 6 ($Threads = new StackableArray();)
  • Me7888
    Me7888 about 10 years
    @MichaelYoo code working after class StackableArray define at Top...but I have problem in After multithreading please see this page : stackoverflow.com/questions/22163063/…
  • Pierre
    Pierre almost 7 years
    What is the variable $re?
  • Michael Yoo
    Michael Yoo almost 7 years
    @Pierre I think I may have used a for() loop before, I think it's meant to just be a [] for array append.
  • LearningPath
    LearningPath almost 6 years
    @MichaelYoo, When calling different APIs with a forEach I believe multithreading is not necessary and asynchronous programming sufficient. i am using a heavy library which is doing synchronous call with c_url (libray called CCXT github.com/ccxt/ccxt ) which is limiting when you have to call 50 api endpoints. we dont want to fork this library and change their http call to asynchronous. How to achieve asynchronous call of instance of this library?. Am i right to thing it is possible with Amp and custom promise?