7

I'm using the pthreads extension for PHP. When I execute the PHP script from cmd on Windows I get parallel threads but when I call the same script from Apache I get a different result and it seems to me like single thread execution.

Is there any configuration that I should make for Apache to get response like cmd (parallel)?

class AsyncOperation extends Thread {
    public function __construct($arg){
        $this->arg = $arg;
    }

    public function run(){
        if($this->arg){
            for($i = 0; $i < 50; $i++) {
                echo "Yoo " . $this->arg . "<br>\n";
            }
        }
    }
}
$thread = new AsyncOperation("World ----------");
$thread2 = new AsyncOperation("Second -------------------------");
$thread->start();
$thread2->start();

for($i = 0; $i < 100; $i++) {
    echo "Standard <br>\n";
}

$thread->join();
$thread2->join();

Example code give response in cmd like:

Yoo World ----------<br>
Yoo World ----------<br>
Yoo World ----------<br>
Standard <br>
Standard <br>
Yoo World ----------<br>
Yoo Second -------------------------<br>
Standard <br>
Standard <br>

In web browser:

Yoo World ----------
Yoo World ----------
Yoo World ----------
Yoo World ----------
...
Yoo Second -------------------------
Yoo Second -------------------------
Yoo Second -------------------------
Yoo Second -------------------------
...
Standard 
Standard 
Standard 
Standard 
...

Update: on different browsers I get different results; this problem might be related to buffer, which I'm going to investigate.

7
  • 1
    have u tried to put 1 sec sleep in each async operation? Commented Mar 1, 2013 at 11:49
  • the results are pretty same. Commented Mar 1, 2013 at 11:57
  • what were you expecting intermixed string output? then split string in thread function, output first string then sleep for few millisecs and output second string. Commented Mar 1, 2013 at 14:56
  • ` public function run(){ if($this->arg){ $strings = explode (' ', $this->arg); for($i = 0; $i < 5; $i++) { echo $strings[0].'<br>\n'; usleep(100000); //100 ms echo $strings[1].'<br>\n'; } } }` Commented Mar 1, 2013 at 15:07
  • Tested with sleep; It didn't work. Apparently PHP/extension can't simulate parallelism. i.e thread scheduling! Commented Mar 1, 2013 at 15:28

1 Answer 1

2

Nothing is simulated, you are executing real threads.

You should not write the standard output from threads in SAPI mode, you will experience unexpected behaviour and errors, that cannot be controlled, there are too many environments and SAPI's to cover it well, so it is not covered at all, don't do it.

Even in CLI mode output of complex code will be garbled, to work around this, you can define a protected method in any object you pass to all contexts that takes care of writing standard output, if the method is protected and the object is a pthreads one, only one context will be able to write standard output at a time ... the same object could be used in the SAPI environment by swapping standard output for a logging database ...

Sign up to request clarification or add additional context in comments.

3 Comments

do you have any example code where i can see how exactly to do that? i mean if there are good practice topic i would like to read them.
stackoverflow.com/a/14565559/1658631 contains an example where output to console is synchronized using protected methods, there are many examples included in the distribution of pthreads covering a wide variety of topics.
@EmrahMehmedov you can also sink the result to stackable and use print_r to see each step how it was processed .... Nothing is simulated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.