0

当使用连接作为继承自 \Thread 的类的字段时,我在 $this->connection->channel() 调用中收到以下错误:

警告:fwrite() 期望参数 1 是资源,整数在第 65 行的 /var/content-generator/PHP/vendor/videlalvaro/php-amqplib/PhpAmqpLib/Wire/IO/StreamIO.php 中给出

如果我使用本地变量,一切正常,但我一转到现场电话就收到错误消息。

失败代码:

public function run()
{
    $this->run = true;
    echo ' Thread-'.$this->ThreadId." including", "\n";
    require_once($this->loader);
    $this->connection = GetRabbitConnection();
    echo ' Thread-'.$this->ThreadId." opening channel", "\n";
    $this->channel = $this->connection->channel();
    echo ' Thread-'.$this->ThreadId." getting queue data", "\n";

    $RedisClient = GetRedisClient();

    $ScrapeExchange = $RedisClient->get(Scrape.":".Exchange);
    $ScrapeQueue = $RedisClient->get(Scrape.":".Queue);

    $this->OutboundExchange = $RedisClient->get(Extract.":".Exchange);
    $this->OutboundRoutingKey = $RedisClient->get(Extract.":".RoutingKey);

    $RedisClient = null;

    echo ' Thread-'.$this->ThreadId." consuming", "\n";

    $this->channel->basic_qos(0,1,false);
    $this->channel->basic_consume($ScrapeQueue, $ScrapeExchange, false, true, false, false, array($this, 'ProcessMessage'));

    while($this->run) {
        $this->channel->wait();
    }

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

工作代码:

public function run()
{
    echo ' Thread-'.$this->ThreadId." including", "\n";
    require_once($this->loader);
    echo ' Thread-'.$this->ThreadId." building connection", "\n";
    $connection = GetRabbitConnection();
    echo ' Thread-'.$this->ThreadId." opening channel", "\n";
    $channel = $connection->channel();

    echo ' Thread-'.$this->ThreadId." getting queue data", "\n";

    $RedisClient = GetRedisClient();

    $ScrapeExchange = $RedisClient->get(Scrape.":".Exchange);
    $ScrapeQueue = $RedisClient->get(Scrape.":".Queue);

    $this->OutboundExchange = $RedisClient->get(Extract.":".Exchange);
    $this->OutboundRoutingKey = $RedisClient->get(Extract.":".RoutingKey);

    $RedisClient = null;

    echo ' Thread-'.$this->ThreadId." consuming", "\n";

    $channel->basic_consume($ScrapeQueue, $ScrapeExchange, false, true, false, false, array($this, 'ProcessMessage'));

    while(true) {
        $channel->wait();
    }

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

我错过了什么?我缺少一些 \Thread 或 pthreads 吗?

4

2 回答 2

1

资源不受官方支持,这些对象依赖于资源。

您已经找到了解决方案:使用方法范围变量,也可以使用静态(类)范围变量。

于 2015-08-14T09:09:59.840 回答
0

PHP 的 PThreads 在实例化和启动之间编组对象变量的方式存在问题。我最终使用了在run()保存对象变量的函数中实例化的对象来完成工作,而不是尝试使用线程对象本身,并且此后没有任何问题。

于 2015-08-13T16:53:24.297 回答