1.第一个hello word

1.生产者

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello')

channel.basic_publish(exchange='',
                      routing_key='hello',
                      body='Hello World!')
print(" [x] Sent 'Hello World!'")
connection.close()

2. 消费者

#!/usr/bin/env python
import pika

connection = pika.BlockingConnection(pika.ConnectionParameters(host='localhost'))
channel = connection.channel()


channel.queue_declare(queue='hello')

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

channel.basic_consume(callback,
                      queue='hello',
                      no_ack=True)

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

 

相关文章:

  • 2021-09-06
  • 2022-01-13
  • 2021-10-16
  • 2019-12-08
  • 2022-12-23
  • 2021-12-02
  • 2021-09-12
  • 2021-07-20
猜你喜欢
  • 2021-07-13
  • 2021-10-15
  • 2021-05-23
  • 2021-10-07
  • 2021-10-09
  • 2022-02-27
相关资源
相似解决方案