首页 文章

fwrite()期望参数1是资源,当访问从局部变量移动到继承自\ Thread的对象字段时,PhpAMQP中给出的整数

提问于
浏览
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?

2 回答

  • 1

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

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

  • 0

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

相关问题