Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

method_frame always return None when using channel.basic_get#469

amitli1 started this conversation inGeneral
Discussion options

I am trying to consume several messages fromRabbitMQ queue without success.

I want to usebasic_consume function and if there are more messages then pull them (instead of being called again to the callback function).

    import pika    import time            class RabbitMQPublisher:        def __init__(self, queue_name):            self.queue_name = queue_name            self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))            self.channel    = self.connection.channel()            self.channel.queue_declare(queue=self.queue_name)            def publish_messages(self, num_messages):            for i in range(num_messages):                message = f'Message {i+1}'                self.channel.basic_publish(exchange='', routing_key=self.queue_name, body=message)                print(f'Sent: {message}')            def close_connection(self):            self.connection.close()        class RabbitMQConsumer:        def __init__(self, queue_name):            self.queue_name = queue_name            self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))            self.channel    = self.connection.channel()            self.channel.queue_declare(queue=self.queue_name)            def callback(self, ch, method, properties, body):            print(f'Received: {body.decode()}')            ch.basic_ack(delivery_tag=method.delivery_tag)            self.pull_remaining_messages()            def pull_remaining_messages(self):            method_frame, header_frame, body = self.channel.basic_get(queue=self.queue_name, auto_ack=False)            print(f"method_frame = {method_frame}")            while method_frame:                print(f'---> [pull_remaining_messages] Received: {body.decode()}')                self.channel.basic_ack(delivery_tag=method_frame.delivery_tag)                method_frame, header_frame, body = self.channel.basic_get(queue=self.queue_name, auto_ack=False)            def start_consuming(self):            self.channel.basic_consume(queue=self.queue_name, on_message_callback=self.callback, auto_ack=False)            print('Waiting for messages. To exit press CTRL+C')            self.channel.start_consuming()        if __name__ == "__main__":            publisher = RabbitMQPublisher('test_queue')        publisher.publish_messages(100)        publisher.close_connection()            time.sleep(2)            consumer = RabbitMQConsumer('test_queue')        consumer.start_consuming()

Using the code above - I always getmethod_frame = None.

What am I missing ?

You must be logged in to vote

Replies: 1 comment

Comment options

basic_get is polling and it must not be used outside of basic integration tests. It will returnNone or its equivalent when there are no messages ready for delivery in the queue. See tutorial 2 and the docguide on Publisher Confirms and Consumer Acknowledgements.

I repeat: DO NOT usebasic.get in production, it defeats all benefits of messaging protocols.

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
General
Labels
None yet
2 participants
@amitli1@michaelklishin

[8]ページ先頭

©2009-2025 Movatter.jp