本文整理汇总了Python中pika.adapters.BlockingConnection类的典型用法代码示例。如果您正苦于以下问题:Python BlockingConnection类的具体用法?Python BlockingConnection怎么用?Python BlockingConnection使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BlockingConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: blocking_submitter
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')
示例2: RabbitMQProcessor
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()
示例3: test_blocking_send_get
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"
示例4: main
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()
示例5: test_blocking_send_get
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"
示例6: test_blocking_invalid_exchange
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"
示例7: test_blocking_send_get
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"
示例8: start
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")
示例9: main
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)
示例10: main
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()
示例11: RabbitMQProcessor
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()
示例12: __init__
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)
示例13: __init__
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)
示例14: emit
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()
示例15: __init__
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)