当前位置: 首页>>代码示例>>Python>>正文


Python Producer.publish方法代码示例

本文整理汇总了Python中kombu.messaging.Producer.publish方法的典型用法代码示例。如果您正苦于以下问题:Python Producer.publish方法的具体用法?Python Producer.publish怎么用?Python Producer.publish使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在kombu.messaging.Producer的用法示例。


在下文中一共展示了Producer.publish方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: UdpService

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
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()
开发者ID:rdudyala,项目名称:ceilometer_custom_plugin,代码行数:62,代码来源:udpservice.py

示例2: QueuePush

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
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')
开发者ID:MeTrina,项目名称:websockframework,代码行数:30,代码来源:QueuePush.py

示例3: test_publish__consume

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    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()
开发者ID:abourget,项目名称:kombu,代码行数:27,代码来源:test_transport_pyredis.py

示例4: test_produce_consume

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def test_produce_consume(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer1 = Consumer(channel, self.q)
        consumer2 = Consumer(channel, self.q2)
        self.q2(channel).declare()

        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory2")

        _received1 = []
        _received2 = []

        def callback1(message_data, message):
            _received1.append(message)
            message.ack()

        def callback2(message_data, message):
            _received2.append(message)
            message.ack()

        consumer1.register_callback(callback1)
        consumer2.register_callback(callback2)

        consumer1.consume()
        consumer2.consume()

        while 1:
            if len(_received1) + len(_received2) == 20:
                break
            self.c.drain_events()

        self.assertEqual(len(_received1) + len(_received2), 20)

        # compression
        producer.publish({"compressed": True},
                         routing_key="test_transport_memory",
                         compression="zlib")
        m = self.q(channel).get()
        self.assertDictEqual(m.payload, {"compressed": True})

        # queue.delete
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")
        self.assertTrue(self.q(channel).get())
        self.q(channel).delete()
        self.q(channel).declare()
        self.assertIsNone(self.q(channel).get())

        # queue.purge
        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory2")
        self.assertTrue(self.q2(channel).get())
        self.q2(channel).purge()
        self.assertIsNone(self.q2(channel).get())
开发者ID:abourget,项目名称:kombu,代码行数:59,代码来源:test_transport_memory.py

示例5: _publish

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
 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()
开发者ID:flaper87,项目名称:kombu,代码行数:12,代码来源:pidbox.py

示例6: _publish_reply

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
 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()
开发者ID:flaper87,项目名称:kombu,代码行数:12,代码来源:pidbox.py

示例7: test_publish__get

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    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())
开发者ID:kazeevn,项目名称:botan,代码行数:13,代码来源:test_transport_pyredis.py

示例8: test_purge

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    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()
开发者ID:crankycoder,项目名称:zamboni-lib,代码行数:13,代码来源:test_transport_redis.py

示例9: producer

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def producer(self):
        self.connection = BrokerConnection(hostname=AMQP_IP, port=AMQP_PORT,
                                           userid=AMQP_USER, password=AMQP_PASSWORD,
                                           virtual_host="/",
                                           transport=AMQP_TRANSPORT)
        self.channel = self.connection.channel()
        # produce

        self.media_exchange = Exchange("media", "direct", durable=True)

        producer = Producer(self.channel, exchange=self.media_exchange, serializer="json")
        producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})

        print self.connection.get_transport_cls()
开发者ID:occi,项目名称:TSP-CloudStack,代码行数:16,代码来源:connection_amqp.py

示例10: test_produce_consume_noack

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def test_produce_consume_noack(self):
        channel = self.c.channel()
        producer = Producer(channel, self.e)
        consumer = Consumer(channel, self.q, no_ack=True)

        for i in range(10):
            producer.publish({"foo": i}, routing_key="test_transport_memory")

        _received = []

        def callback(message_data, message):
            _received.append(message)

        consumer.register_callback(callback)
        consumer.consume()

        while 1:
            if len(_received) == 10:
                break
            self.c.drain_events()

        self.assertEqual(len(_received), 10)
开发者ID:abourget,项目名称:kombu,代码行数:24,代码来源:test_transport_memory.py

示例11: run

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
 def run(self):
   amqhost = self.config_parser.get(self.section,'amqhost')
   amqport = int(self.config_parser.get(self.section,'amqport'))
   amquser = self.config_parser.get(self.section,'amquser')
   amqpass = self.config_parser.get(self.section,'amqpass')
   amqvhost = self.config_parser.get(self.section,'amqvhost')
   amqexchange = self.config_parser.get(self.section,'amqexchange')
   routing_key = self.config_parser.get(self.section,'routing_key')
   connection = BrokerConnection(hostname=amqhost, port=amqport,
                           userid=amquser, password=amqpass,
                           virtual_host=amqvhost)
   channel = connection.channel()
   exchange = Exchange(amqexchange,'topic',turable=True)
   producer = Producer(channel,exchange=exchange, 
                          routing_key=routing_key)
   global entire_history
   entire_history = self.config_parser.get(self.section,'entire_history')
   for job in jobs():
      if job['type']=='start':
         job['completion_time']+=job['walltime']
      producer.revive(channel)
      producer.publish(job)
      logging.debug(`job`)
开发者ID:jmlowe,项目名称:HPC-Stats,代码行数:25,代码来源:pbspump.py

示例12: test_publish__consume

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def test_publish__consume(self):
        connection = Connection(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.assertIn(channel, channel.connection.cycle._channels)
        try:
            connection.drain_events(timeout=1)
            self.assertTrue(_received)
            with self.assertRaises(socket.timeout):
                connection.drain_events(timeout=0.01)
        finally:
            channel.close()
开发者ID:66ru,项目名称:kombu,代码行数:26,代码来源:test_redis.py

示例13: handle

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def handle(self, *args, **options):
        routing_key = options["routing_key"]
        connection_url = options["connection_url"]
        userid = urlparse(connection_url).username
        payload_file = options["payload_file"]

        exchange_name = "exchange/{}/jobs".format(userid)

        connection = Connection(connection_url)
        exchange = Exchange(exchange_name, type="topic")
        producer = Producer(connection,
                            exchange,
                            routing_key=routing_key,
                            auto_declare=True)

        self.stdout.write("Published to exchange: {}".format(exchange_name))

        with open(payload_file) as f:
            body = f.read()

            try:
                producer.publish(body)
            finally:
                connection.release()
开发者ID:edmorley,项目名称:treeherder,代码行数:26,代码来源:publish_to_pulse.py

示例14: test_publish

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
    def test_publish(self):
        channel = self.connection.channel()
        p = Producer(channel, self.exchange, serializer="json")
        message = {u"the quick brown fox": u"jumps over the lazy dog"}
        ret = p.publish(message, routing_key="process")
        self.assertIn("prepare_message", channel)
        self.assertIn("basic_publish", channel)

        m, exc, rkey = ret
        self.assertDictEqual(message, anyjson.loads(m["body"]))
        self.assertDictContainsSubset({"content_type": "application/json",
                                       "content_encoding": "utf-8",
                                       "priority": 0}, m)
        self.assertDictContainsSubset({"delivery_mode": 2}, m["properties"])
        self.assertEqual(exc, p.exchange.name)
        self.assertEqual(rkey, "process")
开发者ID:AshishNamdev,项目名称:mozillians,代码行数:18,代码来源:test_messaging.py

示例15: producer

# 需要导入模块: from kombu.messaging import Producer [as 别名]
# 或者: from kombu.messaging.Producer import publish [as 别名]
 def producer(self):
     producer = Producer(self.channel, exchange=self.media_exchange, serializer="json")
     #producer.exchange.delete()
     producer.publish({"name": "/tmp/lolcat1.avi", "size": 1301013})
     pass
开发者ID:occi,项目名称:TSP-CloudStack,代码行数:7,代码来源:fanout_exchange.py


注:本文中的kombu.messaging.Producer.publish方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。