【发布时间】: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