What is the meaning of $channel->wait() in RabbitMQ

838 views
Skip to first unread message

Core Test

unread,
Jan 20, 2017, 7:38:41 AM1/20/17
to rabbitmq-users
I am quite new in RabbitMQ. I'm working with php-amqplib library with codeigniter, and still wondering about some knowledge which I am lacking.

 - Why `$channel->wait()` is used? What is the actual purpose
 - Why `$channel->wait()` is used? What is the actual purpose it is
    serving? Why it is always reside inside an endless while loop?
 - How to bypass the Infinite while loop when I am using the
    `$channel->basic_consume` in another for loop for generating 5     
    consumers in one shot.

Here is my code snippet:

    public function campaign2(){
           
for( $i=1;$i<=5;$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});  
       
}


 If anyone kindly guide me through the process I will be grateful.

Michael Klishin

unread,
Jan 20, 2017, 7:49:49 AM1/20/17
to rabbitm...@googlegroups.com, Core Test
Because concurrency in PHP is a complicated topic, php-amqplib authors decided to make it single threaded,
which means at certain points you have to tell the library to loop and wait for a response method to arrive
from RabbitMQ. I assume this involves deliveries, which are server pushed.

See https://github.com/php-amqplib/php-amqplib/blob/master/PhpAmqpLib/Channel/AbstractChannel.php#L306.

In many clients you register a consumer and I/O is handled in separate threads (or similar) and thus you don't need
to tell the library to wait.

Loops in long running consumer apps/scripts are often placed at the end to avoid natural program termination.
> --
> You received this message because you are subscribed to the Google Groups "rabbitmq-users"
> group.
> To unsubscribe from this group and stop receiving emails from it, send an email to rabbitmq-user...@googlegroups.com.
> To post to this group, send an email to rabbitm...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

--
MK

Staff Software Engineer, Pivotal/RabbitMQ


Core Test

unread,
Jan 20, 2017, 8:17:56 AM1/20/17
to rabbitmq-users, tester...@gmail.com
Ok. I got your point.

If multi-threading is not possible with php-amqplib, is multi processing is supported. I need to have the concurrent workers those who consumes separately from separate queues parallelly.

I know introducing a for loop is not a good idea. I need to go through some other plan.

This is the scenario I want.

PRODUCER 1 --------------------------------------------QUEUE 1 (1000K)-------------------------------------------------CONSUMER 1

PRODUCER 2 --------------------------------------------QUEUE 2 (100) ----------------------------------------------------CONSUMER 2

PRODUCER 3 --------------------------------------------QUEUE 3 (3000)----------------------------------------------------CONSUMER 3

The structure I am currently having is single queue, and so the second and third user is waiting for a very long time as all of their data has been appended to the last of the 1000K queue.

Can you send me a suggestion.
Reply all
Reply to author
Forward
0 new messages