3
我目前正在使用PHP學習多線程。我已經知道多線程的基本原理,例如創建線程和將多個線程一起運行,但我仍然對工作和池感到困惑。使用pthreads的PHP中的工作者和池
目前,我有這樣的腳本:
<?php
class MyWork extends Threaded {
public $name;
public function __construct($name) {
echo "Constructing worker $name\n";
$this->name = $name;
}
public function run() {
echo "Worker $this->name start running\n";
for ($i = 1; $i <= 5; $i++) {
echo "Worker $this->name : $i\n";
sleep(1);
}
}
}
class MyWorker extends Worker {
public function run() {}
}
$pool = new Pool(1, \MyWorker::class);
$pool->submit(new MyWork("A"));
$pool->submit(new MyWork("B"));
$pool->submit(new MyWork("C"));
隨着我的理解,這應該創建工人池1名工人在一個正確的時間運行的最大值是多少?因此,我希望這種類型的輸出:
Constructing worker A
Constructing worker B
Constructing worker C
Worker A start running
Worker A : 1
Worker A : 2
Worker A : 3
Worker A : 4
Worker A : 5
Worker B start running
Worker B : 1
Worker B : 2
Worker B : 3
Worker B : 4
Worker B : 5
Worker C start running
Worker C : 1
Worker C : 2
Worker C : 3
Worker C : 4
Worker C : 5
但是,這裏是我得到的,而不是:
Constructing worker A
Constructing worker B
Worker A start running
Constructing worker C
Worker A : 1
Worker : 2
Worker : 3
Worker : 4
Worker : 5
Worker start running
Worker : 1
Worker : 2
Worker : 3
Worker : 4
Worker : 5
Worker start running
Worker : 1
Worker : 2
Worker : 3
Worker : 4
Worker : 5
爲什麼工人的名字只出現過一次,然後消失這樣呢?
注意:我在Windows 8.1上使用PHP 5.5.10和phtreads 2.0.4,並通過CLI運行腳本。