fwrite() 期望参数 1 是资源,当访问从局部变量移动到从 \Thread 继承的对象字段时,在 PhpAMQP 中给出的整数
fwrite() expects parameter 1 to be resource, integer given in PhpAMQP when access moved from local variable to field of object inheriting from \Thread
当使用连接作为继承自 \Thread 的 class 的字段时,我在 $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 是我遗漏的?
PHP 的 PThreads 在实例化和启动之间编组对象变量的方式存在问题。我最终使用在 run()
函数中实例化的对象来保存对象变量来完成工作,而不是尝试使用线程对象本身,并且此后没有任何问题。
资源不受官方支持,这些对象依赖于资源。
您已经找到了解决方案:使用方法作用域变量,您也可以使用静态(class)作用域变量。
当使用连接作为继承自 \Thread 的 class 的字段时,我在 $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 是我遗漏的?
PHP 的 PThreads 在实例化和启动之间编组对象变量的方式存在问题。我最终使用在 run()
函数中实例化的对象来保存对象变量来完成工作,而不是尝试使用线程对象本身,并且此后没有任何问题。
资源不受官方支持,这些对象依赖于资源。
您已经找到了解决方案:使用方法作用域变量,您也可以使用静态(class)作用域变量。