rabbitMQ简单使用
生活随笔
收集整理的這篇文章主要介紹了
rabbitMQ简单使用
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
#send.py
import pika import sys# 創(chuàng)建連接 connection = pika.BlockingConnection(pika.ConnectionParameters("localhost")) # 創(chuàng)建channel channel = connection.channel() message = " ".join(sys.argv[1:]) or "hello world" # 創(chuàng)建隊(duì)列,并持久化存儲(chǔ) channel.queue_declare(queue="hello", durable=True)channel.basic_publish(exchange='', routing_key="hello", body="message",properties=pika.BasicProperties(delivery_mode=2)) connection.close() #receive.py
import pika import time# 創(chuàng)建連接 connection = pika.BlockingConnection(pika.ConnectionParameters("localhost")) # 創(chuàng)建channel channel = connection.channel() # 創(chuàng)建隊(duì)列 channel.queue_declare(queue="hello", durable=True)# 回調(diào)函數(shù) def callback(ch, method, properties, body):print("[x] Received %r" % (body,))print(type(body))time.sleep(5)print("[x] Done")ch.basic_ack(delivery_tag=method.delivery_tag)# 公平調(diào)度,讓每個(gè)消費(fèi)者執(zhí)行時(shí)間相近 channel.basic_qos(prefetch_count=1)channel.basic_consume(callback, queue="hello") # 阻塞,等待接收 channel.start_consuming()
import pika import sys# 創(chuàng)建連接 connection = pika.BlockingConnection(pika.ConnectionParameters("localhost")) # 創(chuàng)建channel channel = connection.channel() message = " ".join(sys.argv[1:]) or "hello world" # 創(chuàng)建隊(duì)列,并持久化存儲(chǔ) channel.queue_declare(queue="hello", durable=True)channel.basic_publish(exchange='', routing_key="hello", body="message",properties=pika.BasicProperties(delivery_mode=2)) connection.close() #receive.py
import pika import time# 創(chuàng)建連接 connection = pika.BlockingConnection(pika.ConnectionParameters("localhost")) # 創(chuàng)建channel channel = connection.channel() # 創(chuàng)建隊(duì)列 channel.queue_declare(queue="hello", durable=True)# 回調(diào)函數(shù) def callback(ch, method, properties, body):print("[x] Received %r" % (body,))print(type(body))time.sleep(5)print("[x] Done")ch.basic_ack(delivery_tag=method.delivery_tag)# 公平調(diào)度,讓每個(gè)消費(fèi)者執(zhí)行時(shí)間相近 channel.basic_qos(prefetch_count=1)channel.basic_consume(callback, queue="hello") # 阻塞,等待接收 channel.start_consuming()
?
轉(zhuǎn)載于:https://www.cnblogs.com/akuma233/p/9630004.html
《新程序員》:云原生和全面數(shù)字化實(shí)踐50位技術(shù)專家共同創(chuàng)作,文字、視頻、音頻交互閱讀總結(jié)
以上是生活随笔為你收集整理的rabbitMQ简单使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库中的事务
- 下一篇: UOJ.386.[UNR #3]鸽子固定