【问题标题】:PHP pthreads, in pool tasks, ob_flush and flush cause crashPHP pthreads,在池任务中,ob_flush 和 flush 导致崩溃
【发布时间】:2015-08-07 15:27:49
【问题描述】:
    $p = new Pool(10);

    for ($i = 0; i<1000; i++){
        $tasks[i] = new workerThread($i);
    }

    foreach ($tasks as $task) {
        $p->submit($task);
    }

    // shutdown will wait for current queue to be completed
    $p->shutdown();
    // garbage collection check / read results
    $p->collect(function($checkingTask){
        return ($checkingTask->isGarbage);
    });


class workerThread extends Collectable {

    public function __construct($i){
        $this->i= $i;
    }

    public function run(){
        echo $this->i;
        ob_flush();
        flush();
    }
}

上面的代码是一个简单的例子,会导致崩溃。我正在尝试通过放置 ob_flush(); 和 flush(); 来实时更新页面在线程对象中,它主要按预期工作。所以上面的代码不能保证每次都会崩溃,但是如果你多运行几次,有时脚本会停止并且 Apache 会重新启动并显示错误消息“httpd.exe Application error The instruction at "0x006fb17f" referenced memory at "0x028a1e20 “。内存无法“写入”。点击确定。”

我认为这是由于多个线程尝试同时刷新时的刷新冲突引起的?我可以做些什么来解决它并在有任何新输出时刷新。

【问题讨论】:

    标签: php apache pthreads threadpool flush


    【解决方案1】:

    多线程不应该写标准输出,这样做没有安全的方法。

    Zend 没有提供确保其安全的设施,它的工作是巧合,而且总是不安全的。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-05-27
      • 2011-05-10
      • 1970-01-01
      • 2011-01-24
      • 2023-03-09
      • 1970-01-01
      相关资源
      最近更新 更多