本文整理汇总了Python中kombu.messaging.Producer类的典型用法代码示例。如果您正苦于以下问题:Python Producer类的具体用法?Python Producer怎么用?Python Producer使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Producer类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: UdpService
class UdpService(os_service.Service):
"""Listener for the udpservice service."""
def start(self):
"""Bind the UDP socket and handle incoming data."""
# ensure dispatcher is configured before starting other services
super(UdpService, self).start()
if cfg.CONF.udpservice.udp_address:
self.tg.add_thread(self.start_udp)
def convert_sample_to_event_data(self,msg):
event_data = {'event_type': 'infra','message_id':six.text_type(uuid.uuid4()),'publisher_id': 'cpe_publisher_id','timestamp':datetime.datetime.now().isoformat(),'priority':'INFO','payload':msg}
return event_data
def setup_rabbit_mq_channel(self):
service_exchange = Exchange(cfg.CONF.udpservice.acord_control_exchange, "topic", durable=False)
rabbit_host = cfg.CONF.udpservice.rabbit_hosts
rabbit_user = cfg.CONF.udpservice.rabbit_userid
rabbit_password = cfg.CONF.udpservice.rabbit_password
# connections/channels
connection = BrokerConnection(rabbit_host, rabbit_user, rabbit_password)
print 'Connection to RabbitMQ server successful'
channel = connection.channel()
# produce
self.producer = Producer(channel, exchange=service_exchange, routing_key='notifications.info')
def start_udp(self):
address_family = socket.AF_INET
if netutils.is_valid_ipv6(cfg.CONF.udpservice.udp_address):
address_family = socket.AF_INET6
udp = socket.socket(address_family, socket.SOCK_DGRAM)
udp.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
udp.bind((cfg.CONF.udpservice.udp_address,
cfg.CONF.udpservice.udp_port))
self.setup_rabbit_mq_channel()
self.udp_run = True
while self.udp_run:
# NOTE(jd) Arbitrary limit of 64K because that ought to be
# enough for anybody.
data, source = udp.recvfrom(64 * units.Ki)
try:
sample = msgpack.loads(data, encoding='utf-8')
except Exception:
LOG.warn(_("UDP: Cannot decode data sent by %s"), source)
else:
try:
if sample.has_key("event_type"):
LOG.debug(_("recevied event :%s"),sample)
self.producer.publish(sample)
else:
LOG.debug(_("recevied Sample :%s"),sample)
msg = self.convert_sample_to_event_data(sample)
self.producer.publish(msg)
except Exception:
LOG.exception(_("UDP: Unable to store meter"))
def stop(self):
self.udp_run = False
super(UdpService, self).stop()
示例2: test_publish__consume
def test_publish__consume(self):
connection = BrokerConnection(transport=Transport)
channel = connection.channel()
producer = Producer(channel, self.exchange, routing_key="test_Redis")
consumer = Consumer(channel, self.queue)
producer.publish({"hello2": "world2"})
_received = []
def callback(message_data, message):
_received.append(message_data)
message.ack()
consumer.register_callback(callback)
consumer.consume()
self.assertTrue(channel._poller._can_start())
try:
connection.drain_events(timeout=1)
self.assertTrue(_received)
self.assertFalse(channel._poller._can_start())
self.assertRaises(socket.timeout,
connection.drain_events, timeout=0.01)
finally:
channel.close()
示例3: _InitConnect
def _InitConnect(self):
if self.connection is None or self.connection.connected==False:
self.connection = Connection(hostname=self.server,port=self.port,userid=self.usr,password=self.psw,virtual_host=self.path)
self.channel = self.connection.channel()
self.producer=Producer(self.channel)
self.smsExchange=Exchange("sys.sms",type='topic',channel=self.channel,durable=True,delivery_mode=2)
self.smsCodeProduce=Producer(self.channel,self.smsExchange,routing_key='sms.code')
示例4: QueuePush
class QueuePush(object):
def __init__(self,Queue_Server,Queue_Port,Queue_User,Queue_PassWord,Queue_Path):
self.usr=Queue_User
self.psw=Queue_PassWord
self.server=Queue_Server
self.port=Queue_Port
self.path=Queue_Path
self.connection=None
self.smsExchange=None
def _InitConnect(self):
if self.connection is None or self.connection.connected==False:
self.connection = Connection(hostname=self.server,port=self.port,userid=self.usr,password=self.psw,virtual_host=self.path)
self.channel = self.connection.channel()
self.producer=Producer(self.channel)
self.smsExchange=Exchange("sys.sms",type='topic',channel=self.channel,durable=True,delivery_mode=2)
self.smsCodeProduce=Producer(self.channel,self.smsExchange,routing_key='sms.code')
def Push(self,queueid,connectid,body):
self.rawPush(queueid,{'connid':connectid},body)
def Close(self,queueid,connectid):
self.rawPush(queueid,{'connid':connectid,'close_connect':'1'},'close')
def rawPush(self,routing_key,headers,body):
self._InitConnect()
self.producer.publish(body=body,delivery_mode=2,headers=headers,
routing_key=routing_key,retry=True,compression='gzip')
def sendCode(self,phone,code):
self._InitConnect()
json_str=json.dumps({'phone':str(phone),"content":u"您的莱信验证码为:%s,请在5分钟内输入完成验证。【莱福思】"%str(code)},ensure_ascii=False)
self.smsCodeProduce.publish(body=json_str,retry=True,compression='gzip')
示例5: test_revive
def test_revive(self):
chan = self.connection.channel()
p = Producer(chan)
chan2 = self.connection.channel()
p.revive(chan2)
self.assertIs(p.channel, chan2)
self.assertIs(p.exchange.channel, chan2)
示例6: test_prepare
def test_prepare(self):
message = {u"the quick brown fox": u"jumps over the lazy dog"}
channel = self.connection.channel()
p = Producer(channel, self.exchange, serializer="json")
m, ctype, cencoding = p._prepare(message, headers={})
self.assertDictEqual(message, anyjson.loads(m))
self.assertEqual(ctype, "application/json")
self.assertEqual(cencoding, "utf-8")
示例7: test_manual_declare
def test_manual_declare(self):
channel = self.connection.channel()
p = Producer(channel, self.exchange, auto_declare=False)
self.assertTrue(p.exchange.is_bound)
self.assertNotIn("exchange_declare", channel,
"auto_declare=False does not declare exchange")
p.declare()
self.assertIn("exchange_declare", channel,
"p.declare() declares exchange")
示例8: _publish_reply
def _publish_reply(self, reply, exchange, routing_key, channel=None):
chan = channel or self.connection.channel()
try:
exchange = Exchange(
exchange, exchange_type="direct", delivery_mode="transient", durable=False, auto_delete=True
)
producer = Producer(chan, exchange=exchange)
producer.publish(reply, routing_key=routing_key)
finally:
channel or chan.close()
示例9: _publish
def _publish(self, type, arguments, destination=None, reply_ticket=None, channel=None):
message = {"method": type, "arguments": arguments, "destination": destination}
if reply_ticket:
message["reply_to"] = {"exchange": self.reply_exchange.name, "routing_key": reply_ticket}
chan = channel or self.connection.channel()
producer = Producer(chan, exchange=self.exchange)
try:
producer.publish(message)
finally:
channel or chan.close()
示例10: test_prepare_compression
def test_prepare_compression(self):
message = {u"the quick brown fox": u"jumps over the lazy dog"}
channel = self.connection.channel()
p = Producer(channel, self.exchange, serializer="json")
headers = {}
m, ctype, cencoding = p._prepare(message, compression="zlib",
headers=headers)
self.assertEqual(ctype, "application/json")
self.assertEqual(cencoding, "utf-8")
self.assertEqual(headers["compression"], "application/x-gzip")
self.assertEqual(simplejson.loads(m.decode("zlib")), message)
示例11: test_publish__get
def test_publish__get(self):
channel = self.connection.channel()
producer = Producer(channel, self.exchange, routing_key="test_Redis")
self.queue(channel).declare()
producer.publish({"hello": "world"})
self.assertDictEqual(self.queue(channel).get().payload, {"hello": "world"})
self.assertIsNone(self.queue(channel).get())
self.assertIsNone(self.queue(channel).get())
self.assertIsNone(self.queue(channel).get())
示例12: test_purge
def test_purge(self):
channel = self.connection.channel()
producer = Producer(channel, self.exchange, routing_key="test_Redis")
self.queue(channel).declare()
for i in range(10):
producer.publish({"hello": "world-%s" % (i, )})
self.assertEqual(channel._size("test_Redis"), 10)
self.assertEqual(self.queue(channel).purge(), 10)
channel.close()
示例13: test_prepare_compression
def test_prepare_compression(self):
message = {u'the quick brown fox': u'jumps over the lazy dog'}
channel = self.connection.channel()
p = Producer(channel, self.exchange, serializer='json')
headers = {}
m, ctype, cencoding = p._prepare(message, compression='zlib',
headers=headers)
self.assertEqual(ctype, 'application/json')
self.assertEqual(cencoding, 'utf-8')
self.assertEqual(headers['compression'], 'application/x-gzip')
import zlib
self.assertEqual(anyjson.loads(
zlib.decompress(m).decode('utf-8')), message)
示例14: test_prepare_custom_content_type
def test_prepare_custom_content_type(self):
message = 'the quick brown fox'.encode('utf-8')
channel = self.connection.channel()
p = Producer(channel, self.exchange, serializer='json')
m, ctype, cencoding = p._prepare(message, content_type='custom')
self.assertEqual(m, message)
self.assertEqual(ctype, 'custom')
self.assertEqual(cencoding, 'binary')
m, ctype, cencoding = p._prepare(message, content_type='custom',
content_encoding='alien')
self.assertEqual(m, message)
self.assertEqual(ctype, 'custom')
self.assertEqual(cencoding, 'alien')
示例15: test_prepare_is_already_unicode
def test_prepare_is_already_unicode(self):
message = u'the quick brown fox'
channel = self.connection.channel()
p = Producer(channel, self.exchange, serializer='json')
m, ctype, cencoding = p._prepare(message, content_type='text/plain')
self.assertEqual(m, message.encode('utf-8'))
self.assertEqual(ctype, 'text/plain')
self.assertEqual(cencoding, 'utf-8')
m, ctype, cencoding = p._prepare(message, content_type='text/plain',
content_encoding='utf-8')
self.assertEqual(m, message.encode('utf-8'))
self.assertEqual(ctype, 'text/plain')
self.assertEqual(cencoding, 'utf-8')