无法修改扩展Thread的类的属性

时间:2015-12-18 19:06:17

标签: php pthreads

我正在编写一个小项目,主进程在RabbitMQ上订阅并等待消息传来。然后有一个Thread保持按相关ID分组的相关响应队列,并且一旦从第一个响应开始经过3秒,它将通过套接字连接将它们推送到另一个组件。问题是,无论我尝试什么,队列都不会被修改。例如,当我从RabbitMQ收到消息时,我调用update_queue,传递消息并尝试使用响应更新队列。但是队列始终为空。

       <?php

ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);

require_once __DIR__ . '/vendor/autoload.php';
use PhpAmqpLib\Connection\AMQPStreamConnection;
use PhpAmqpLib\Message\AMQPMessage;

class Aggregator {

    private $task;

    public function __construct(){
        $this->task = new Task();
        $this->task->start();
    }

    public function subscribe(){
        $connection = new AMQPStreamConnection('localhost', 5672, 'guest', 'guest');
        $channel = $connection->channel();

        $channel->queue_declare('aggregator', false, false, false, false);
        $channel->basic_consume('aggregator', '', false, true, false, false, array($this, 'on_response'));

        echo ' [*] Waiting for messages. To exit press CTRL+C', "\n";

        while(count($channel->callbacks)) {
            $channel->wait();
        }

        $channel->close();
        $connection->close();
    }

    public function on_response($msg){
        echo "Pushing to queue";
        $this->task->update_queue($msg);
    }
}

class Task extends Thread {

    private $queue;
    private $socket;
    const TIMEOUT = 3;
    //Flag used to pause queue check when its being updated
    private $pause;


    public function __construct(){
        $this->queue = array();
        $this->pause = false;
        //$this->socket = stream_socket_client('localhost:9999/echo');
    }

    public function update_queue($msg){
        echo "Updating queue \n";
        $this->pause = true;
        $response = json_decode($msg->body);

        if(!array_key_exists($msg->get('correlation_id'), $this->queue)){
            //Gets in here and correlation_id is a non-null value
            echo "Correlation does not exist" . $msg->get('correlation_id').  "\n";
            $this->queue[$msg->get('correlation_id')] = array();
            $this->queue[$msg->get('correlation_id')]['created_at'] = time();
            $this->queue[$msg->get('correlation_id')]['response'] = array();
        }

        //Null here
        var_dump($this->queue[$msg->get('correlation_id')]);

        //Response is a non-nul value
        $this->queue[$msg->get('correlation_id')]['response'][] = $response;
        $this->pause = false;

        //Always 0 here
        echo "Count: " . count($this->queue) . "\n";

        echo "Queue updated \n";
    }

    public function __destruct(){
        //fclose($this->socket);
    }

    public function run(){

        while(true){
            if($this->pause){continue;}

            //Always 0 here!
            //echo "Count: " . count($this->queue) . "\n";


            foreach($this->queue as $k => $v){
                echo $v['created_at'] . "\n";
                if(self::TIMEOUT + $v['created_at'] < time()){

                    $response = array();
                    $response['correlation_id'] = $k;
                    $response['response'] = $v['response'];

                    var_dump($response);

                    $response = json_encode($response);
                    //fwrite($this->sockett, $response);
                    //unset($this->queue[$k]);
                }
            }
        }
    }
}

$aggregator = new Aggregator();
$aggregator->subscribe();
?>

0 个答案:

没有答案