Jul 1, 2024
pip install pika
.producer.py
import pika
connection_parameters = pika.ConnectionParameters('localhost')
connection = pika.BlockingConnection(connection_parameters)
channel = connection.channel()
channel.queue_declare(queue='letterbox')
message = 'Hello, this is my first message'
channel.basic_publish(exchange='', routing_key='letterbox', body=message)
print(f"Sent message: {message}")
connection.close()
consumer.py
import pika
connection_parameters = pika.ConnectionParameters('localhost')
connection = pika.BlockingConnection(connection_parameters)
channel = connection.channel()
channel.queue_declare(queue='letterbox')
def on_message_received(ch, method, properties, body):
print(f"Received new message: {body}")
channel.basic_consume(queue='letterbox', on_message_callback=on_message_received, auto_ack=True)
print("Started consuming")
channel.start_consuming()
python consumer.py
python producer.py
ممنون از تماشای شما!