dongtingxiao4697 2016-12-16 16:27
浏览 68
已采纳

pthreads挂起/卡在pool-> submit

I'm trying to get Pthreads to work, but unfortunately with all of the outdated documentation it is really hard to figure out what is current and what has changed. As well as not getting any errors returned.

Running PHP7.2.0-dev with the latest version of pthreads and xDebug

Currently I have

<?php

$THREADS = 3;

class UptimeWorker extends Worker {
    public function start(?int $options = null) {
        return parent::start(PTHREADS_INHERIT_CLASSES | PTHREADS_INHERIT_CONSTANTS | PTHREADS_INHERIT_FUNCTIONS);
    }
}

class UptimeWork extends Threaded {

    private $i;

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

    public function run() {
        echo "I am ".$i.PHP_EOL;
        $this->setGarbage();
    }

}

$pool = new Pool($THREADS, UptimeWorker::class);

for($i=0;$i<10;$i++) {
    var_dump($i);
    $pool->submit(new UptimeWork($i));
}

echo "pre-shutdown".PHP_EOL;

$pool->shutdown();

echo "finished".PHP_EOL;

and the only output is int(0) from the first var_dump call in the for statement, from there the process just seems to hang, it doesn't exit and no further output is given.

What is going wrong here and what needs to happen in order for this to work?

  • 写回答

2条回答 默认 最新

  • douzhai7873 2016-12-18 23:16
    关注

    I get two errors in your code, both in

     public function run() {
        echo "I am ".$i.PHP_EOL;
        $this->setGarbage();
    }
    

    First it should the $this->i (simple typo)

    Second is that setGarbage is not found as it's a property of Collectable (http://php.net/manual/en/collectable.setgarbage.php - Note: the manual documents the old PHP 5/API version 2 classes, this has changed to an interface for PHP 7/API version 3). Therefore you need

    class UptimeWork extends Threaded  Implements Collectable {
    

    to get access to setGarbage(). As Collectable is an interface, you also need to write the interface methods.

    Here is your working code:

    $THREADS = 3;
    
    class UptimeWorker extends Worker {
        public function start(?int $options = null) {
            return parent::start(PTHREADS_INHERIT_CLASSES | PTHREADS_INHERIT_CONSTANTS | PTHREADS_INHERIT_FUNCTIONS);
        }
    }
    
    
    class UptimeWork extends Threaded  Implements Collectable {
    
        private $i;
        private $isGarbage = false;
    
        public function __construct(int $i) {
            $this->i = $i;
        }
    
        public function run() {
            echo "I am ".$this->i.PHP_EOL;
            $this->setGarbage();
        }
    
        public function setGarbage() {
            echo "Setting garbage ".$this->i.PHP_EOL;
            $this->isGarbage = true;
        }
    
        public function isGarbage() : bool {
            return $this->isGarbage;
        }
    
    }
    
    $pool = new Pool($THREADS, UptimeWorker::class);
    
    for($i=0;$i<10;$i++) {
        var_dump($i);
        $pool->submit(new UptimeWork($i));
    }
    
    echo "pre-shutdown".PHP_EOL;
    
    $pool->shutdown();
    
    echo "finished".PHP_EOL;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用