0

我正在尝试将一个简单的 AMQP 客户端变成多线程。如果我不使用线程扩展发布,则以下代码有效:

<?php

include ('../JS-amqp-include.php');

$mypid = getmypid();

echo "PID: $mypid\n";

class Publish extends Thread {

    private $co;// connection
    private $ch;// channel
    private $ex;// exchange
    private $mypid, $thread_id;

    public function __construct($connection = '') {
        $this->mypid = getmypid();

        $this->co = new AMQPConnection();
        $this->co->setHost(HOST);
        $this->co->setLogin(USER);
        $this->co->setPassword(PASS);
        $this->co->setVHost(VHOST);
        $this->co->connect();           // <-- Fail here!!!!

        $this->ch = new AMQPChannel($this->co);

        $this->ex = new AMQPExchange($this->ch);
        $this->ex->setName(X_DIR);
        $this->ex->setType(XT_DIR);
        $this->ex->setFlags(AMQP_DURABLE);
        $this->ex->declareExchange();
    }

    public function run($cycle = 10, $input = '') {
        for ($i = 1; $i <= $cycle; $i++) {
            $msg = $this->mypid.':'.$input.':'.$i;
            $this->ex->publish($msg);
        }
    }
}

$time_start = microtime(true);

$pub = new Publish();
$pub->run(100, 'thread');
$time_end      = microtime(true);
$time_duration = $time_end-$time_start;
echo "Duration: $time_duration Sec.\n";

?>;

以下是有错误的输出:

PID: 2276
PHP Fatal error:  Uncaught exception 'AMQPConnectionException' with message 'Socket error: could not connect to host.' in /home/john/rmq/php/Amqp/JS-amqp-publish-thread.php:24
Stack trace:
#0 /home/john/rmq/php/Amqp/JS-amqp-publish-thread.php(24): AMQPConnection->connect()
#1 /home/john/rmq/php/Amqp/JS-amqp-publish-thread.php(45): Publish->__construct()
#2 {main}
  thrown in /home/john/rmq/php/Amqp/JS-amqp-publish-thread.php on line 24

任何人都知道为什么会发生这种情况或如何解决?

Rabbitmq 在 HOST 上运行。

如果从类发布声明中删除“扩展线程”,该程序将完美运行。上面的代码甚至还没有使用线程。我只是扩展课程并且它打破了。

系统信息

操作系统:Ubuntu 15.10

Rabbitmq-c (apt-get 安装)

librabbitmq-dev:amd64 0.5.2-2 amd64 AMQP 客户端库用 C 编写 - 开发文件 librabbitmq1:amd64 0.5.2-2 amd64 AMQP 客户端库用 C 编写

我按照此链接重新编译 Ubuntu PHP 包以支持 ZTS。

PHP 5.6.11-1ubuntu3.2 (cli)

版权所有 (c) 1997-2015 PHP 集团

Zend Engine v2.6.0,版权所有 (c) 1998-2015 Zend Technologies 和 Zend OPcache v7.0.6-dev,版权所有 (c) 1999-2015,Zend Technologies

PHP 模块

amqp 1.6.0 稳定版

pthreads 2.0.10 稳定版

4

0 回答 0