电竞比分网-中国电竞赛事及体育赛事平台

分享

RabbitMQ消息隊(duì)列

 心靜水境 2018-03-19
#/usr/bin/env python
# -*- coding:utf-8 -*-

import pika
# credentials = pika.PlainCredentials('policymaker','policymaker')
# connection = pika.BlockingConnection(pika.ConnectionParameters(
# 'localhost',5672,'/',credentials))
# # ---------------------------------------------------------------------------------------
#
# def sender():
# connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
# channel = connection.channel()
# channel.queue_declare(queue='hello')
# channel.basic_publish(exchange='',
# routing_key='hello',
# body='Hello, World!')
# print('send msg: Hello World!')
# connection.close()
#
# def receiver():
# connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
# channel = connection.channel()
# channel.queue_declare(queue='hello')
# def callback(ch, method, properties, body):
# print('receive msg: %s' % body)
# ch.basic_ack(delivery_tag = method.delivery_tag) # 告訴生成者,消息處理完成
# channel.basic_consume(callback,
# queue='hello',
# no_ack=False)
# print('waiting for msg...')
# channel.start_consuming()
# receiver()
# sender()
# ---------------------------------------------------------------------------------------
class RabbitMQ(object):
def __init__(self,queue):
self.queue = queue # 隊(duì)列名稱
self.connection = pika.BlockingConnection(pika.ConnectionParameters('localhost'))
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.queue)
def callbalk(self,ch,method,properties,body):
print(body)
ch.basic_ack(delivery_tag=method.delivery_tag) # 告訴生產(chǎn)者,消息處理完成
def sender(self):
self.channel.basic_publish(exchange='',routing_key='hello',body='hi') # routing_key:發(fā)送消息到的隊(duì)列
self.connection.close()
def receiver(self):
self.channel.basic_consume(self.callbalk,queue=self.queue,no_ack=False)
print('wating...')
self.channel.start_consuming()


rabbitmq = RabbitMQ('helly')
rabbitmq.sender()
# rabbitmq.receiver()

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多