Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php $channel->;的含义是什么;RabbitMQ中的wait()_Php_Codeigniter_Consumer_Php Amqplib - Fatal编程技术网

Php $channel->;的含义是什么;RabbitMQ中的wait()

Php $channel->;的含义是什么;RabbitMQ中的wait(),php,codeigniter,consumer,php-amqplib,Php,Codeigniter,Consumer,Php Amqplib,我对拉比犬很陌生。我正在使用带有codeigniter的php amqplib库工作,并且仍然想知道我所缺乏的一些知识 为什么使用$channel->wait() 为什么它总是驻留在一个无止境的while循环中 如何绕过无限while循环 就像我的项目中的一个用户想要向10万个潜在客户广播新活动一样,如果第二个用户有大约100封邮件要发送,那么第二个用户必须等待10万封邮件先发送,然后轮到最后一个用户 我需要一个解决方案的并发消费者,谁工作顺利,而不影响其他 以下是我的代码片段: publi

我对拉比犬很陌生。我正在使用带有codeigniter的php amqplib库工作,并且仍然想知道我所缺乏的一些知识

  • 为什么使用
    $channel->wait()
  • 为什么它总是驻留在一个无止境的while循环中
  • 如何绕过无限while循环
就像我的项目中的一个用户想要向10万个潜在客户广播新活动一样,如果第二个用户有大约100封邮件要发送,那么第二个用户必须等待10万封邮件先发送,然后轮到最后一个用户

我需要一个解决方案的并发消费者,谁工作顺利,而不影响其他

以下是我的代码片段:

public function campaign2(){
        $this->load->library('mylibrary');
        for( $i=1;$i<=5;$i++ ) {
            $url = "http://localhost/myproject/rabbit/waiting";
            $param = array('index' => $i);
            $this->waiting($i);
        }          
}

public function waiting($i)
    {
        ini_set('memory_limit','400M');
        ini_set('max_execution_time', 0);
        ini_set('display_errors', 1);

        ${'conn_'.$i} = connectRabbit();
        ${'channel_'.$i} = ${'conn_'.$i}->channel();
        ${'channel_'.$i}->exchange_declare('ha-local-campaign-'.$i.'-exchange', 'fanout', false, true, false);
        $q    = populateQueueName('campaign-'.$i);
        ${'channel_'.$i}->queue_declare($q, false, true, false, false); 
        ${'channel_'.$i}->queue_bind($q, 'ha-local-campaign-'.$i.'-exchange', 'priority.'.$i);
        $consumer_tag = 'campaign_consumer' ;
        function process_message($msg) {
            echo 'Mail Sent';
            $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
        }
        function shutdown($channel, $conn){
            echo '['.date('H:i:s').'] Campaign consumer - Shutdown!!'; 
        }

        ${'channel_'.$i}->basic_consume($q, $consumer_tag, false, false, true, false,'process_message');
        while(1) {
           ${'channel_'.$i}->wait();
        }
        register_shutdown_function('shutdown', ${'channel_'.$i}, ${'conn_'.$i});  
    }
公共职能活动2(){
$this->load->library('mylibrary');
对于($i=1;$i$i);
$this->waiting($i);
}          
}
公共职能部门(一美元)
{
ini_设置(“内存限制”,“400M”);
ini_集('max_execution_time',0);
ini设置(“显示错误”,1);
${'conn.'$i}=connectRabbit();
${'channel.'$i}=${'conn.'$i}->channel();
${'channel'.$i}->exchange_declare('ha-local-campaign-'.$i.-exchange','fanout',false,true,false);
$q=populateQueueName('campaign-'。$i);
${'channel.'$i}->queue_declare($q,false,true,false,false);
${'channel'.$i}->queue'u bind($q,'ha local campaign-'.$i.'-exchange,'priority'.$i);
$consumer_tag='campaign_consumer';
函数进程消息($msg){
回显“已发送邮件”;
$msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
}
功能关闭($channel,$conn){
echo“[”.date('H:i:s').]活动消费者-关闭!!”;
}
${'channel.'$i}->basic_consume($q,$consumer_标记,false,false,true,false,'process_message');
而(1){
${'channel.'$i}->wait();
}
寄存器_shutdown_函数('shutdown'、${'channel_uu'.$i}、${'conn'.$i});
}

如果有人能指导我完成这一过程,我将不胜感激。

当您呼叫
$channel->wait()
时,您是:

  • 检查通道队列以查看是否存在挂起的消息

  • 对于每条消息,您将调用相应频道回调的已注册的回调

从“hello world示例”中,一步一步:

// First, you define `$callback` as a function receiving
// one parameter (the _message_).
$callback = function($msg) {
  echo " [x] Received ", $msg->body, "\n";
};

// Then, you assign `$callback` the the "hello" queue.
$channel->basic_consume('hello', '', false, true, false, false, $callback);

// Finally: While I have any callbacks defined for the channel, 
while(count($channel->callbacks)) {
    // inspect the queue and call the corresponding callbacks
    //passing the message as a parameter
    $channel->wait();
}
// This is an infinite loop: if there are any callbacks,
// it'll run forever unless you interrupt script's execution.
让您的第二个用户使用不同的队列发送。您可以拥有任意数量的队列。

这是模拟队列

channel.start_consuming()

在python(pika库)中

编码提示:PHP没有嵌套函数的概念,因此您的
process\u message
shutdown
函数只是普通的全局函数,而不是此方法或类的一部分;如果您两次调用
waiting
方法,您将得到一个错误,因为您将使用相同的名称声明它们两次。您可能需要或调用方法。此外,除了变量(
${'conn.'$i}=connectRabbit();
)之外,只需使用数组(
$connections=array();…$connections[$i]=connectRabbit();
)。我保证,你会发现使用它更容易。我认为$channel->wait()就像一个无限循环,因为我在这个调用之前和while循环中放了一个echo“message”,但它只渲染一次