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 基于双目测规则物体尺寸
  • ¥15 wegame打不开英雄联盟
  • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
  • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
  • ¥30 eclipse开启服务后,网页无法打开
  • ¥30 雷达辐射源信号参考模型
  • ¥15 html+css+js如何实现这样子的效果?
  • ¥15 STM32单片机自主设计
  • ¥15 如何在node.js中或者java中给wav格式的音频编码成sil格式呢
  • ¥15 不小心不正规的开发公司导致不给我们y码,