dongzouhe9734 2015-06-25 17:21
浏览 82
已采纳

PHP pThreads IIS:浏览器中的意外输出

I'm attempting to have a php script run multi-threaded using PHP pThreads in a IIS 7 PHP 5.6 x86 (thread safe) environment. I've created a github issue but I haven't heard back and though some other coders here may have experienced the same problem.

PHP is running fine and scripts which don't require pThreads execute like normal. However when I add some multi-threaded code to my script, only the block of code which is non-multithreaded executes in the browser unless I call phpinfo before executing the code. For example:

<?php
class AsyncOperation extends Thread {

  public function run(){
    echo 'hello world';
  }

}

$thread = new AsyncOperation();

if($thread->start()) {
    $thread->join();
}
?>

Has no output in the browser (still a response code of 200 though).

But the following code with phpinfo called first works:

<?php
phpinfo();

class AsyncOperation extends Thread {

  public function run(){
    echo 'hello world';
  }

}
$thread = new AsyncOperation();
if($thread->start()) {
    $thread->join();
}
?>

The expected output of all of the phpinfo content, and then hello world is there. I've also noticed that sending phpinfo(64) does not echo hello world, but all of the other valid parameters for phpinfo cause hello world to be printed.

I've also tried running the original (without phpinfo) script from the command line and noticed that "hello world" is echoed in the console. Which leads me to believe this is an IIS7 misconfiguration but that doesn't explain how calling phpinfo would make it work?

Also I should note, there's nothing in the error logs either, and the system is set to log all errors.

  • 写回答

1条回答 默认 最新

  • dqyl2374 2015-07-02 14:09
    关注

    As krakjoe mentioned in your issue, strange things can happen when different threads work with the same output buffer.

    It's probably best to follow JakeFalcor's advice and first store the thread's output in memory and then echo it from the main thread.

    class AsyncOperation extends Thread {
    
      public $output;
    
      public function run(){
        $this->output = 'hello world';
      }
    
    }
    
    $thread = new AsyncOperation();
    
    if($thread->start()) {
        $thread->join();
        echo $thread->output;
    }
    

    See also: ob_start()

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题