Tell me more ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

This question already has an answer here:

I am new to php and trying to run three functions in parallel. I have a code similar to the following:

Call function1(…….);  //all these function are located in separate host server
Call function2(…….);  
Call function3(…….);

All the above functions will be running for 5 min or more. Therefore, I really need to call them in parallel, if not my program will run for 15 min or more. any help would be greatly appreciated.

share|improve this question
PHP is not a threaded/parallellizable language. Run 3 separate PHP scripts, each calling one of those functions. – Marc B Jun 4 at 20:05
Without knowing what exactly are 'functions' in your example, it's hard to recommend anything. In some cases you may need to look for something like Gearman, in some there might be already some 'pooling' technique implemented in your particular tools of trade. – raina77ow Jun 4 at 20:06
There are just too many duplicates for this question. Research first, ask later. – Maxim Kumpan Jun 4 at 20:08

marked as duplicate by Wouter J, FreshPrinceOfSO, Fabio, alecxe, Roman C Jun 5 at 8:32

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1 Answer

up vote 2 down vote accepted

You can use pThread , here is a good place to start :

Example

$ts = array();
$ts[] = new Call("function1");
$ts[] = new Call("function2");
$ts[] = new Call("function3");

foreach($ts as $t) {
    $t->start();
}

foreach($ts as $t) {
    $t->join();
}

Simple Thread Class

class Call extends Thread {

    function __construct($func) {
        $this->func = $func;
    }

    function run() {
        call_user_func($this->func);
    }
}
share|improve this answer
Note: pThread is experimental. – Frank Farmer Jun 4 at 20:19
2  
Using it in production for a while now .... – Baba Jun 4 at 20:21
Thank you so much for explaning this to me in detail, I will study this more. really appriciate your help. – Justin k Jun 4 at 20:33

Not the answer you're looking for? Browse other questions tagged or ask your own question.