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

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.

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用 PHP pThreads PHP 5.6 x86(线程安全)环境。 我已经创建了一个 github问题,但我没有收到回复,而且还有一些其他编码器 这里可能遇到了同样的问题。

PHP运行正常,不需要pThreads的脚本正常执行。 但是,当我向我的脚本添加一些多线程代码时,只有非多线程代码块在浏览器中执行,除非我在执行代码之前调用phpinfo。 例如:

 &lt;?php 
class AsyncOperation扩展线程{
 
公共函数run(){
 echo'hello world'; 
} 
  
 
 
 
 $ thread = new AsyncOperation(); 
 
if($ thread-&gt; start()){
 $ thread-&gt; join(); 
} 
?&gt; \  n   
 
 

浏览器中输出(但仍然是响应代码200)。

但下面的代码与phpinfo称为第一个有效:

 &lt;?php 
phpinfo(); 
 
class AsyncOperation扩展Thread {
  
公共函数run(){
 echo'hello world'; 
} 
 
} 
 $ thread = new AsyncOperation(); 
if($ thread-&gt; start()){
 $  thread-&gt; join(); 
} 
?&gt; 
   
 
 

所有phpinfo内容的预期输出,然后是hello world。 我还注意到发送 phpinfo(64)不会回显hello world,但phpinfo的所有其他有效参数都会导致打印hello world。

我也尝试从命令行运行原始(没有phpinfo)脚本,并注意到“hello world”在控制台中回显。 这让我相信这是一个IIS7配置错误,但这并不能解释如何调用phpinfo使它工作?

另外我应该注意,错误日志中也没有,并且 system设置为记录所有错误。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dqyl2374
    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()

    点赞 评论

相关推荐