如何使用 PHP pthreads 删除对可堆叠对象的所有引用?
how do I remove all references to stackable objects with PHP pthreads?
我正在 PHP 中试验 pthreads。参考 Joe 的 gist 覆盖池,我创建了一个工作线程池,并提交了可堆叠对象的实例。线程和工作都按照我的预期进行,但是似乎主要 thread/context 没有(完全)取消引用 Stackable 对象,因此内存使用量会无限增加,因为它会保留越来越多的实例。
它似乎 clear/nullify 可堆叠对象的所有属性,因此内存使用量不会 迅速 达到顶峰,但它确实会永远增加,所以这实际上不能用于 long-运行 过程。我希望我只是做错了什么。下面是演示我的意思的示例代码:
class W extends Worker {
public function run(){}
}
class S extends Stackable {
public $id;
public function run() {
$this->id = uniqid();
}
public function __destruct () {
// $this->id will be null here when the main thread destroys its copy of this
if ($this->id === null) {
echo "nullified stackable destroyed\n";
} else {
echo "stackable {$this->id} destroyed\n";
}
}
}
$pool = new Pool(3, W::class);
$i = 0;
while ($i++ < 10) {
$pool->submit(new S());
}
sleep(1); // to let threads finish
$pool->collect(function (S $s) {
echo "collected stackable {$s->id}\n";
return true;
});
$pool->shutdown();
echo "script exit - here come the destructions of all the accumulated stackables\n";
作为 PHP7 升级的一部分,pthreads v3 有许多改进。
您会发现此代码的 PHP7 版本按预期工作:
<?php
class W extends Worker {
public function run(){}
}
class S extends Threaded implements Collectable {
public $id;
public function run() {
$this->id = uniqid();
$this->garbage = true;
}
public function __destruct () {
if ($this->id === null) {
echo "nullified stackable destroyed\n";
} else {
echo "stackable {$this->id} destroyed\n";
}
}
public function isGarbage() : bool { return $this->garbage; }
private $garbage = false;
}
$pool = new Pool(3, W::class);
$i = 0;
while ($i++ < 10) {
$pool->submit(new S());
}
while ($pool->collect(function (S $s) {
if ($s->isGarbage()) {
echo "collected stackable {$s->id}\n";
}
return $s->isGarbage();
})) continue;
$pool->shutdown();
echo "script exit - here come the destructions of all the accumulated stackables\n";
建议新项目使用PHP7和pthreads v3,它比PHP5和pthreads v2.
优越得多
我正在 PHP 中试验 pthreads。参考 Joe 的 gist 覆盖池,我创建了一个工作线程池,并提交了可堆叠对象的实例。线程和工作都按照我的预期进行,但是似乎主要 thread/context 没有(完全)取消引用 Stackable 对象,因此内存使用量会无限增加,因为它会保留越来越多的实例。
它似乎 clear/nullify 可堆叠对象的所有属性,因此内存使用量不会 迅速 达到顶峰,但它确实会永远增加,所以这实际上不能用于 long-运行 过程。我希望我只是做错了什么。下面是演示我的意思的示例代码:
class W extends Worker {
public function run(){}
}
class S extends Stackable {
public $id;
public function run() {
$this->id = uniqid();
}
public function __destruct () {
// $this->id will be null here when the main thread destroys its copy of this
if ($this->id === null) {
echo "nullified stackable destroyed\n";
} else {
echo "stackable {$this->id} destroyed\n";
}
}
}
$pool = new Pool(3, W::class);
$i = 0;
while ($i++ < 10) {
$pool->submit(new S());
}
sleep(1); // to let threads finish
$pool->collect(function (S $s) {
echo "collected stackable {$s->id}\n";
return true;
});
$pool->shutdown();
echo "script exit - here come the destructions of all the accumulated stackables\n";
作为 PHP7 升级的一部分,pthreads v3 有许多改进。
您会发现此代码的 PHP7 版本按预期工作:
<?php
class W extends Worker {
public function run(){}
}
class S extends Threaded implements Collectable {
public $id;
public function run() {
$this->id = uniqid();
$this->garbage = true;
}
public function __destruct () {
if ($this->id === null) {
echo "nullified stackable destroyed\n";
} else {
echo "stackable {$this->id} destroyed\n";
}
}
public function isGarbage() : bool { return $this->garbage; }
private $garbage = false;
}
$pool = new Pool(3, W::class);
$i = 0;
while ($i++ < 10) {
$pool->submit(new S());
}
while ($pool->collect(function (S $s) {
if ($s->isGarbage()) {
echo "collected stackable {$s->id}\n";
}
return $s->isGarbage();
})) continue;
$pool->shutdown();
echo "script exit - here come the destructions of all the accumulated stackables\n";
建议新项目使用PHP7和pthreads v3,它比PHP5和pthreads v2.
优越得多