Server Programming/ETC

RabbitMQ Python 예제

Dev.BeryL 2022. 1. 27. 18:46
728x90

메세지 큐를 송신 및 수신하는 Request, Response.py 를 생성하고 하단과 같이 활용한다.

 

Request.py

import json
import pika
import sys

cred = pika.PlainCredentials('user', 'pwd')
connection = pika.BlockingConnection(
  pika.ConnectionParameters(host='host_ip',credentials=cred))
channel = connection.channel()

channel.exchange_declare(exchange='queue_name')#, exchange_type='fanout')

param = {
  '전달 내용'
}
message = json.dumps(param)
channel.basic_publish(exchange='queue_name', routing_key='', body=message)
print(' [x] Sent %r' % message)
connection.close()

 

 

Response.py

import pika
from pika import credentials

cred = pika.PlainCredentials('user', 'pwd')
connection = pika.BlockingConnection(
  pika.ConnectionParameters(host='host_ip',credentials=cred))
channel = connection.channel()


print(' [*] Waiting for logs. To exit press CTRL+C')

def callback(ch, method, properties, body):
  print(' [x] %r' % body)

channel.basic_consume(
  queue='queue_name', on_message_callback=callback, auto_ack=True)

channel.start_consuming()
반응형

'Server Programming > ETC' 카테고리의 다른 글

API & RESTful API  (0) 2022.02.12
Apache Kafka 및 메세지 큐 종류와 차이점  (0) 2022.01.27
RabbitMQ core, exchange  (0) 2022.01.27
동기와 비동기의 개념과 차이점  (1) 2019.01.24