我遇到了问题:工作线程似乎是连续工作的。 这是一个非常简单的示例,仅用于演示问题。
我初始化我的工作线程并运行它们。它应该并行工作,但结果看起来像是连续处理的。
PHP 5.6.12 , Windows 操作系统, Apache 服务器
DBWorker : Which of course extends pThreads Worker class
class DBWorker extends Worker {
private $str;
public function __construct($str){
$this->str = $str;
}
public function run(){
$i=0;
while($i < 10) {
echo "$this->str: $i\n";
$i++;
}
}
}
初始化:
private $DBWorker1;
private $DBWorker2;
private $DBWorker3;
/* ... */
public function __construct(){
$this->DBWorker1 = new DBWorker("worker1");
$this->DBWorker2 = new DBWorker("worker2");
$this->DBWorker3 = new DBWorker("worker3");
}
/* ... */
$this->DBWorker1->start();
$this->DBWorker2->start();
$this->DBWorker3->start();
$i=0;
while($i < 10) {
echo "parent: $i\n";
$i++;
}
var_dump($this->DBWorker1->shutdown());
var_dump($this->DBWorker2->shutdown());
var_dump($this->DBWorker3->shutdown());
结果:
parent: 0
parent: 1
parent: 2
...
parent: 6
worker1: 0
worker1: 1
worker1: 2
...
worker1: 59
worker2: 0
worker2: 1
worker2: 2
...
worker2: 59
worker3: 0
worker3: 1
worker3: 2
...
worker3: 59
parent: 7
parent: 8
parent: 9
bool(true)
bool(true)
bool(true)