PHP pThreads IIS:浏览器中的意外输出 [英] PHP pThreads IIS: unexpected output in browser

查看:60
本文介绍了PHP pThreads IIS:浏览器中的意外输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PHP pThreads 使php脚本运行多线程.在IIS 7 PHP 5.6 x86(线程安全)环境中.我创建了一个 github问题,但是我没有回音,尽管有其他一些编码员这里可能遇到了同样的问题.

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运行正常,不需要pThreads的脚本可以正常执行.但是,当我在脚本中添加一些多线程代码时,只有非多线程代码块才能在浏览器中执行,除非我在执行代码之前先调用phpinfo.例如:

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();
}
?>

在浏览器中没有输出(尽管响应代码仍为200).

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

但是以下代码首先被phpinfo调用起作用:

<?php
phpinfo();

class AsyncOperation extends Thread {

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

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

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

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.

我还尝试从命令行运行原始脚本(没有phpinfo),并注意到控制台中回显了"hello world".这使我相信这是IIS7的错误配置,但这并不能解释调用phpinfo如何使其工作?

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.

推荐答案

krakjoe在您的问题中提到的,当不同的线程与相同的输出缓冲区.

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

最好遵循 JakeFalcor的建议并首先存储线程的在内存中输出,然后从主线程回显它.

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;
}

另请参见: ob_start()

See also: ob_start()

这篇关于PHP pThreads IIS:浏览器中的意外输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆