Skip to content

RabbitMQ. Basic Subsribe

nagaevkirill edited this page Sep 24, 2023 · 3 revisions
import pika

def on_mess(channel, method_frame, header_frame, body):
    if body.decode("utf-8") == 'procedure':
        print('Процедура взята в очередь')
    else:
        print(f'body is not procedure, body is {type(body)}')
    # channel.basic_ack(delivery_tag=method_frame.delivery_tag)  # можно вручную присвоить данному сообщению статус Acked

creds = pika.credentials.PlainCredentials('user', 'userpass')
connection = pika.BlockingConnection(pika.ConnectionParameters('82.146.**.126', '5672', 'main', creds))
channel = connection.channel()

channel.basic_consume('dev1', on_mess, auto_ack=True)  # обрати внимание стоит параметр auto_ack
try:
    channel.start_consuming()
except KeyboardInterrupt:
    channel.stop_consuming()
except Exception:
    channel.stop_consuming()

print('DONE')

github.com/nagaevkirill/rabbitmq_basic_publish_consume

Clone this wiki locally