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


Python BlockingConnection.channel方法代码示例

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


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

示例1: RabbitMQProcessor

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
class RabbitMQProcessor(CrawlQueue):
    """class created to encapsulate the rabbit reading and rewriting proccess
    to make common interface towards already existing crawle code
    Author: Maximiliano Mendez
    """

    def __init__(self, host, queue_name):
        super(RabbitMQProcessor, self).__init__()
        self.queue_name = queue_name
        self.parameters = pika.ConnectionParameters(host)
        self.connection = BlockingConnection(self.parameters)

        self.channel = self.connection.channel()
        self.channel.queue_declare(queue=self.queue_name, durable=True, exclusive=False, auto_delete=False)

    def _get(self):
        method, header, body = self.channel.basic_get(queue=self.queue_name)

        if method.NAME == "Basic.GetEmpty":
            raise Queue.Empty
        req_res = pickle.loads(body)
        self.channel.basic_ack(delivery_tag=method.delivery_tag)
        return req_res

    def _put(self, req_res):
        message = pickle.dumps(req_res)
        self.channel.basic_publish(
            exchange="",
            routing_key=self.queue_name,
            body=message,
            properties=pika.BasicProperties(content_type="text/plain", delivery_mode=1),
        )

    def stop(self):
        self.connection.close()
开发者ID:popego,项目名称:fetcher,代码行数:37,代码来源:crawle.py

示例2: test_blocking_invalid_exchange

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def test_blocking_invalid_exchange():

    # Connect to RabbitMQ
    connection = BlockingConnection(support.PARAMETERS)

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    queue_name = support.tools.test_queue_name('blocking_send_get')
    channel.queue_declare(queue=queue_name,
                          durable=False,
                          exclusive=True,
                          auto_delete=True)

    message = 'test_blocking_send:%.4f' % time()
    try:
        channel.basic_publish(exchange="invalid-exchange",
                              routing_key=queue_name,
                              body=message,
                              mandatory=True,
                              properties=BasicProperties(
                                      content_type="text/plain",
                                      delivery_mode=1))

        while True:
            channel.transport.connection.process_data_events()

    except AMQPChannelError, err:
        if err[0] != 404:
            assert False, "Did not receive a Channel.Close"
开发者ID:CashStar,项目名称:pika,代码行数:33,代码来源:blocking_invalid_test.py

示例3: main

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def main(argv=None):
    """Main logic hit by the commandline invocation."""
    parser = argparse.ArgumentParser(__doc__)
    parser.add_argument('config', help="path to the configuration file")
    args = parser.parse_args(argv)
    if args.config is not None:
        fileConfig(args.config)
        logger.info("Logging initialized")

    config = ConfigParser()
    config.read(args.config)
    # Grab the database uri setting from the config.
    Session = create_database_session(config.get('anomaly', 'database-uri'))

    # Queue initialization
    connection = BlockingConnection()
    channel = connection.channel()
    # Declare the exchange and an unnamed queue.
    channel.exchange_declare(exchange=EXCHANGE, type='topic')
    declared_queue = channel.queue_declare(queue=QUEUE, durable=True,
                                           exclusive=False)
    channel.queue_bind(exchange=EXCHANGE, queue=QUEUE,
                       routing_key=BINDING_KEY)

    # Setup up our consumer callback
    channel.basic_consume(consumer, queue=QUEUE)

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()
    connection.close()
开发者ID:pumazi,项目名称:anomaly,代码行数:34,代码来源:catchall.py

示例4: blocking_submitter

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def blocking_submitter(parameters, data_queue,
        properties=DEFAULT_PROPERTIES):
    while True:
        try:
            connection = BlockingConnection(parameters)
            channel = connection.channel()
            channel.queue_declare(queue='sandbox', durable=True)
        except Exception:
            log.error('connection failure', exc_info=True)
            time.sleep(1)
            continue
        while True:
            log.info('waiting on data queue')
            try:
                data = data_queue.get(timeout=1)
            except Queue.Empty:
                try:
                    connection.process_data_events()
                except AMQPConnectionError:
                    break
                continue
            log.info('got data to submit')
            try:
                channel.basic_publish(exchange='',
                            routing_key='sandbox',
                            body=data,
                            properties=properties,
                            mandatory=True)
            except Exception:
                log.error('submission failed', exc_info=True)
                data_queue.put(data)
                break
            log.info('submitted data to broker')
开发者ID:gswyhq,项目名称:hello-world,代码行数:35,代码来源:RabbitMQ发布者保持连接.py

示例5: test_blocking_send_get

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def test_blocking_send_get():

    parameters = pika.ConnectionParameters(host=HOST, port=PORT)
    connection = BlockingConnection(parameters)

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    queue_name = support.tools.test_queue_name('blocking_send_get')
    channel.queue_declare(queue=queue_name,
                          durable=False,
                          exclusive=True,
                          auto_delete=True)

    message = 'test_blocking_send:%.4f' % time.time()
    try:
        channel.basic_publish(exchange='undeclared-exchange',
                              routing_key=queue_name,
                              body=message,
                              properties=pika.BasicProperties(
                                content_type="text/plain",
                                delivery_mode=1))
    except pika.exceptions.AMQPChannelError, error:
        if error[0] != 404:
            assert False, "Did not receive a Channel.Close"
开发者ID:bkjones,项目名称:pika,代码行数:28,代码来源:blocking_invalid_test.py

示例6: test_blocking_send_get

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def test_blocking_send_get():

    parameters = pika.ConnectionParameters(host=HOST, port=PORT)
    connection = BlockingConnection(parameters)

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    queue_name = support.tools.test_queue_name('blocking_send_get')
    channel.queue_declare(queue=queue_name,
                          durable=False,
                          exclusive=True,
                          auto_delete=True)

    message = 'test_blocking_send:%.4f' % time.time()
    channel.basic_publish(exchange='',
                          routing_key=queue_name,
                          body=message,
                          properties=pika.BasicProperties(
                            content_type="text/plain",
                            delivery_mode=1))

    # Loop while we try to get the message we sent
    message_in = channel.basic_get(queue=queue_name)

    # Close the connection
    connection.close()

    # Only check the body
    if message_in[2] != message:
        assert False, "Did not receive the same message back"
开发者ID:bkjones,项目名称:pika,代码行数:34,代码来源:blocking_send_get_test.py

示例7: RabbitMQProcessor

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
class RabbitMQProcessor(CrawlQueue):
    """class created to encapsulate the rabbit reading and rewriting proccess
    to make common interface towards already existing crawle code
    Author: Maximiliano Mendez
    """

    def __init__(self, host, queue_name):
        super(RabbitMQProcessor, self).__init__()
        conn_retries = 4
        connected = False
        self.queue_name = queue_name
        while conn_retries > 0 or not connected:
            try:
                self.parameters = pika.ConnectionParameters(host)
                self.connection = BlockingConnection(self.parameters)

                self.channel = self.connection.channel()
                self.channel.queue_declare(queue=self.queue_name, durable=True,
                                   exclusive=False, auto_delete=False)
                connected = True
            except Exception, e:
                conn_retries -= 1

        if not connected:
            global STOP_CRAWLE
            STOP_CRAWLE = True
            self.stop()
开发者ID:maximilianom,项目名称:fetcher,代码行数:29,代码来源:crawle.py

示例8: test_blocking_send_get

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def test_blocking_send_get():

    connection = BlockingConnection(support.PARAMETERS)

    # Open the channel
    channel = connection.channel()

    # Declare the queue
    queue_name = support.tools.test_queue_name('blocking_send_get')
    channel.queue_declare(queue=queue_name,
                          durable=False,
                          exclusive=True,
                          auto_delete=True)

    message = ('test_blocking_send:%.4f' % time()).encode('utf-8')
    channel.basic_publish(routing_key=queue_name,
                          exchange="",
                          body=message,
                          properties=BasicProperties(
                                  content_type="text/plain",
                                  delivery_mode=1))

    # Loop while we try to get the message we sent
    message_in = channel.basic_get(queue=queue_name)

    # Close the connection
    connection.close()

    # Only check the body
    if message_in[2] != message:
        assert False, "Did not receive the same message back"
开发者ID:Kozea,项目名称:pika,代码行数:33,代码来源:blocking_send_get_test.py

示例9: Consumer

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
class Consumer(object):
    def __init__(self, routing_key):
        self.connection = BlockingConnection(
            ConnectionParameters(host='33.33.33.10')
        )
        self.channel = self.connection.channel()
        self.queue = self.channel.queue_declare(queue='test_metranome', exclusive=True, auto_delete=False)
        self.channel.queue_bind(exchange='metranome', queue='test_metranome', routing_key=routing_key)

    def get(self):
        return self.channel.basic_get(queue='test_metranome', no_ack=True)
开发者ID:bkjones,项目名称:metranome,代码行数:13,代码来源:test_amqp_connection.py

示例10: start

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def start(db, messaging):
    global connection
    connection = BlockingConnection(pika.URLParameters(messaging))
    
    global channel
    channel = connection.channel()
    channel.queue_declare(queue=EXCHANGE, durable=True, exclusive=False, auto_delete=False)
    channel.exchange_declare(exchange=EXCHANGE)
    channel.queue_bind(exchange=EXCHANGE, queue=EXCHANGE)

    global database
    client = MongoClient(db)
    database = client.get_default_database()

    app.run(host="0.0.0.0")
开发者ID:rberrelleza,项目名称:crawler,代码行数:17,代码来源:rest.py

示例11: main

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def main(argv=None):
    """Main logic hit by the commandline invocation."""
    connection = BlockingConnection()
    channel = connection.channel()
    channel.queue_declare(queue=QUEUE, durable=True, exclusive=False)

    # Convert the data over to JSON message bits relative to what the
    #   producer would send.
    data = [json.dumps(dict(zip(XXX_DATA_STRUCT, d))) for d in XXX_DATA]
    for message in data:
        # Send the message to the preprocessor/incoming queue.
        properties = BasicProperties(content_type="application/json",
                                     delivery_mode=1)
        channel.basic_publish(exchange='', routing_key=QUEUE, body=message,
                              properties=properties)
开发者ID:pumazi,项目名称:anomaly,代码行数:17,代码来源:manual-test.py

示例12: main

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def main(argv=None):
    """Main logic hit by the commandline invocation."""
    parser = argparse.ArgumentParser(__doc__)
    parser.add_argument("config", help="path to the configuration file")
    parser.add_argument("-n", "--name", help="drone name (used in configuration)")
    args = parser.parse_args(argv)
    if args.config is not None:
        fileConfig(args.config)
        logger.info("Logging initialized")

    config = ConfigParser()
    config.read(args.config)

    # Retrieve the drone's settings from a generic section or one
    #   specified in the arguments.
    config_section = "drone"
    if args.name is not None:
        config_section = "drone:{0}".format(args.name)
    drone_settings = dict(config.items(config_section))

    # XXX Used to initialize a sql session, but this should never
    #     happen because drones shouldn't have access to the
    #     persistent storage.
    Session = create_database_session(config.get("anomaly", "database-uri"))

    # Queue initialization
    connection = BlockingConnection()
    channel = connection.channel()
    # Declare the exchange and an unnamed queue.
    channel.exchange_declare(exchange=EXCHANGE, type="topic")
    declared_queue = channel.queue_declare(exclusive=True)
    queue_name = declared_queue.method.queue
    channel.queue_bind(exchange=EXCHANGE, queue=queue_name, routing_key=drone_settings["binding-key"])

    # Import the consumer from the settings line.
    module_path, consumer_name = drone_settings["consumer-class"].split(":")
    consumer_import = __import__(module_path, globals(), locals(), [consumer_name])
    consumer_cls = getattr(consumer_import, consumer_name)
    consumer = consumer_cls(drone_settings)

    # Setup up our consumer callback
    channel.basic_consume(consumer, queue=queue_name)

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()
    connection.close()
开发者ID:pumazi,项目名称:anomaly,代码行数:50,代码来源:drone.py

示例13: emit

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
    def emit(self, record):
        msg = self.format(record)

        body = json.dumps(
            {"msg": msg, "loggername": record.name, "level": record.levelname, "created": datetime.now().isoformat()}
        )

        try:
            con = BlockingConnection(ConnectionParameters(self.host))
        except socket.error:
            raise RabbitConnectionException("Connection to {0} failed".format(self.host))
        channel = con.channel()

        channel.queue_declare(queue=self.queue, durable=True, exclusive=False, auto_delete=False)

        channel.basic_publish(exchange="", routing_key=self.queue, body=body)
        con.close()
开发者ID:hltbra,项目名称:rapidlog,代码行数:19,代码来源:rabbit.py

示例14: main

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
def main(argv=None):
    """Main logic hit by the commandline invocation."""
    logger.setLevel(logging.INFO)
    connection = BlockingConnection()
    channel = connection.channel()
    # Declare the exchange and an unnamed queue.
    channel.exchange_declare(exchange=EXCHANGE, type='topic')
    declared_queue = channel.queue_declare(exclusive=True)
    queue_name = declared_queue.method.queue
    channel.queue_bind(exchange=EXCHANGE, queue=queue_name,
                       routing_key=BINDING_KEY)

    # Setup up our consumer callback
    channel.basic_consume(consumer, queue=queue_name)

    try:
        channel.start_consuming()
    except KeyboardInterrupt:
        channel.stop_consuming()
    connection.close()
开发者ID:pumazi,项目名称:anomaly,代码行数:22,代码来源:example-consumer.py

示例15: __init__

# 需要导入模块: from pika.adapters import BlockingConnection [as 别名]
# 或者: from pika.adapters.BlockingConnection import channel [as 别名]
class PikaDilly:

    def __init__(self):
        self.exchange_name = "bu_outputs"
        self.queue = "glue_script"
        parameters = pika.ConnectionParameters('localhost')
        self.connection = BlockingConnection(parameters) # Open conn to RabbitMQ with default params for localhost
        self.channel = self.connection.channel() # Open the channel
        self.channel.exchange_declare(exchange=self.exchange_name, type='direct', passive=False)
        self.channel.queue_declare(queue=self.queue, durable=True,exclusive=False, auto_delete=False) # Declare a queue
        self.channel.queue_bind(queue=self.queue, exchange=self.exchange_name, routing_key=self.queue)

    def publisher(self, _file):
        # Send a message
        try:
            m = magic.Magic(mime=True)
            mtype = str(m.from_file(_file))
            self.channel.basic_publish(exchange=self.exchange_name, routing_key=self.queue, body=_file, properties=BasicProperties(content_type=mtype,delivery_mode=2))
        except IOError, err:
            pass
开发者ID:fdunc,项目名称:bungeni-exist,代码行数:22,代码来源:publisher_daemon.py


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