本文整理汇总了Python中pika.BlockingConnection.close方法的典型用法代码示例。如果您正苦于以下问题:Python BlockingConnection.close方法的具体用法?Python BlockingConnection.close怎么用?Python BlockingConnection.close使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika.BlockingConnection
的用法示例。
在下文中一共展示了BlockingConnection.close方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: Service
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class Service( object ):
def __init__( self, groups, host = 'localhost', verbose = True, port = 5672, user = '', password = '' ):
credentials = PlainCredentials(user, password)
self._connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) )
self._channel = self._connection.channel()
self._channel.exchange_declare( exchange = exchange_name, type = 'topic' )
self._queueID = self._channel.queue_declare( exclusive = True ).method.queue
for topic in groups:
self._channel.queue_bind(exchange = exchange_name, queue = self._queueID, routing_key = topic)
def _handle( self, c, m, p, b):
print b
def close( self ):
self._channel.stop_consuming()
print 'done', datetime.datetime.now()
self._connection.close()
def run( self ):
self._channel.basic_consume( self._handle, queue = self._queueID, no_ack = True )
self._channel.start_consuming()
示例2: TopicClient
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class TopicClient(object):
host = config["RABBIT_HOST"]
port = config["RABBIT_PORT"]
exchange = config["EXCHANGE"]
def __init__(self):
self.conn = BlockingConnection(ConnectionParameters(host=self.host, port=self.port))
self.channel = self.conn.channel()
self.channel.exchange_declare(exchange=self.exchange, type="topic")
def subscribe_to_topic(self, callback, topic):
result = self.channel.queue_declare(exclusive=True)
queue_name = result.method.queue
self.channel.queue_bind(exchange=self.exchange, queue=queue_name, routing_key=topic)
self.channel.basic_consume(callback, queue=queue_name, no_ack=True)
def publish_to_topic(self, message, topic):
self.channel.basic_publish(exchange=self.exchange, routing_key=topic, body=message)
def run(self):
print "Start something"
try:
self.channel.start_consuming()
except KeyboardInterrupt:
print "Closing"
self.channel.stop_consuming()
self.conn.close()
示例3: Service
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class Service( object ):
def __init__( self, topics, name, host = 'localhost', verbose = True, port = 5672, user = '', password = '' ):
credentials = PlainCredentials(user, password)
self.connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) )
self.channel = self.connection.channel()
self.channel.exchange_declare( exchange = exchange_name, type = 'topic' )
self.queueID = self.channel.queue_declare( exclusive = True ).method.queue
self.name = name
for topic in topics:
self.channel.queue_bind( exchange = exchange_name, queue = self.queueID, routing_key = topic)
def _handle( self, c, m, p, b ):
routingKey = self.name #'.'.join( [ 'SS', self.name ] )
print routingKey, b
self.channel.basic_publish( exchange = exchange_name, routing_key = routingKey, body = b)
def close( self ):
self.channel.stop_consuming()
print 'done', datetime.datetime.now()
#for key, val in self._deposit.iteritems():
# print key, len( val )
self.connection.close()
def run( self ):
#_callback = lambda c, m, p, d: self._handle( d )
self.channel.basic_consume( self._handle, queue = self.queueID, no_ack = True )
self.channel.start_consuming()
示例4: Publisher
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class Publisher(object):
connection = None
channel = None
exchange = None
def __init__(self, props):
self.props = props
def start(self, exchange):
self.exchange = exchange
self.connection = BlockingConnection()
self.connection.set_backpressure_multiplier(self.props.backpressure)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=exchange, durable=True, exclusive=False, auto_delete=False)
def publish(self, status):
self.channel.basic_publish(
exchange="",
routing_key=self.exchange,
body=status,
properties=BasicProperties(content_type="text/plain", delivery_mode=1),
)
def close(self):
self.connection.close()
示例5: work_queue
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def work_queue():
message = ' '.join(sys.argv[1:]) or "Hello World!"
connection = BlockingConnection(ConnectionParameters('localhost'))
channel = connection.channel()
queue_name = 'work_queue'
# create a work queue and send a message directly to it, bypassing the exchange
channel.queue_declare(queue='work_queue', durable=True)
channel.basic_publish(exchange='', routing_key='work_queue', body=message, properties=BasicProperties(delivery_mode=2))
print " [x] Sent '%s'" % (message)
connection.close()
示例6: post
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def post(self, message: str, queue_name: str):
"""
Posts the given message to a queue with the given name via the message broker's default exchange.
:param message: the message to post
:param queue_name: the name of the queue to post to
"""
connection = BlockingConnection(self._connection_parameters)
try:
channel = connection.channel()
channel.basic_publish(exchange="", routing_key=queue_name, body=message)
finally:
connection.close()
示例7: Service
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class Service( object ):
def __init__( self, host = 'localhost', port = 5672, user = '', password = '', vhost = '/', routingKey = ''):
credentials = PlainCredentials( user, password )
self._connection = BlockingConnection( ConnectionParameters( host, port, vhost, credentials ) )
#self._connection = SelectConnection( ConnectionParameters( host, port, vhost, credentials ) )
self._channel = self._connection.channel()
self._channel.exchange_declare( exchange = exchange_name, type = 'topic' )
self.rkey = routingKey
def close( self ):
self._connection.close()
def run( self ):
#message = raw_input("Message : ")
while True:
message = """
XKRX-CS-KR-000252,13:30:48.023942,7,290.9,123.19,90.82,79.62,937.15
XKRX-CS-KR-000253,13:30:48.024171,7,28.84,93.29,67.13,234.64,149.7
XKRX-CS-KR-000254,13:30:48.024337,7,248.17,118.49,1489.54,118.45,117.42
XKRX-CS-KR-000255,13:30:48.024497,7,70.67,170.82,65.45,152.11,420.7
XKRX-CS-KR-000256,13:30:48.034801,7,160.74,82.36,260.87,104.42,384.35
XKRX-CS-KR-000257,13:30:48.034973,7,123.39,150.31,60.78,201.21,181.55
XKRX-CS-KR-000100,13:30:48.035137,8,166.66,87.45,252.83,82.03,44.02
XKRX-CS-KR-000101,13:30:48.045434,8,114.86,1023.0,37.92,65.76,61.82
XKRX-CS-KR-000102,13:30:48.045586,8,159.16,97.96,60.07,75.29,690.15
XKRX-CS-KR-000103,13:30:48.045730,8,23.52,133.91,44.0,107.83,533.96
XKRX-CS-KR-000104,13:30:48.045901,8,76.62,274.25,166.57,116.48,149.1
XKRX-CS-KR-000250,13:30:48.056203,8,105.32,254.87,158.97,21.0,59.72
XKRX-CS-KR-000251,13:30:48.056364,8,192.7,226.26,76.02,72.7,40.53
XKRX-CS-KR-000252,13:30:48.056520,8,138.58,138.76,89.68,41.78,175.83
XKRX-CS-KR-000253,13:30:48.066883,8,88.67,41.84,126.81,222.26,8.98
XKRX-CS-KR-000254,13:30:48.067103,8,156.14,126.11,46.24,24.03,57.94
XKRX-CS-KR-000255,13:30:48.067259,8,136.01,35.25,25.29,275.88,50.33
XKRX-CS-KR-000256,13:30:48.067416,8,136.89,10.51,197.03,200.62,238.65
XKRX-CS-KR-000257,13:30:48.077776,8,47.36,41.77,101.75,105.99,64.56
XKRX-CS-KR-000100,13:30:48.078006,9,26.76,231.9,104.19,117.87,24.69
XKRX-CS-KR-000101,13:30:48.078187,9,57.14,84.92,73.62,33.72,47.86
XKRX-CS-KR-000102,13:30:48.088561,9,21.85,120.6,538.69,58.24,1685.93
XKRX-CS-KR-000103,13:30:48.088819,9,450.32,417.01,210.68,121.41,27.18
XKRX-CS-KR-000104,13:30:48.088998,9,80.61,69.15,132.51,98.67,226.2
XKRX-CS-KR-000250,13:30:48.089161,9,107.44,11.22,80.1,85.93,125.1
XKRX-CS-KR-000251,13:30:48.099518,9,43.86,51.79,282.43,101.35,946.29
XKRX-CS-KR-000252,13:30:48.099705,9,170.75,242.6,74.15,323.43,28.48
XKRX-CS-KR-000253,13:30:48.099871,9,53.27,36.47,81.75,50.96,46.73
XKRX-CS-KR-000254,13:30:48.110195,9,136.93,17.66,77.64,253.57,66.8
XKRX-CS-KR-000255,13:30:48.110408,9,65.49,72.59,39.59,63.07,74.31
XKRX-CS-KR-000256,13:30:48.110575,9,63.16,44.29,36.04,119.36,21.78
XKRX-CS-KR-000257,13:30:48.110733,9,125.17,54.65,374.91,219.27,136.63
"""
self._channel.basic_publish( exchange = exchange_name, routing_key = self.rkey, body = message )
print 'Done', datetime.datetime.now(), ", Message :", message
self.close()
示例8: fanout_exchange
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def fanout_exchange():
message = ' '.join(sys.argv[1:]) or "Hello World!"
connection = BlockingConnection(ConnectionParameters('localhost'))
channel = connection.channel()
exchange_name = 'analytics'
# create a fanout exchange
channel.exchange_declare(exchange=exchange_name, type='fanout')
# send a task
channel.basic_publish(exchange=exchange_name, routing_key='', body=message)
print " [x] Sent '%s'" % (message)
connection.close()
示例9: direct_exchange
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def direct_exchange():
severity = sys.argv[1]
message = ' '.join(sys.argv[2:]) or "Hello World!"
message = severity + ": " + message
connection = BlockingConnection(ConnectionParameters('localhost'))
channel = connection.channel()
# create a direct exchange
channel.exchange_declare(exchange='direct_logs', type='direct')
# send a task
channel.basic_publish(exchange='direct_logs', routing_key=severity, body=message)
print " [x] Sent '%s'" % (message)
connection.close()
示例10: BlockingChannel
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class BlockingChannel:
"""Provides raw blocking Pika channel object for given connection id.
The created connection is closed automatically after 'with' statement has
been closed.
Usage::
from collective.zamqp.connection import BlockingChannel
with BlockingChannel(connection_id) as channel:
frame = channel.declare_queue(auto_delete=True)
# ...
Refer to Pika's API on how to use the raw channel-object. E.g. you
could use it to declare a channel to retrieve the amount of messages
still waiting on that channel.
"""
def __init__(self, connection_id, timeout=60):
# Look up the given connection
connection = getUtility(IBrokerConnection, name=connection_id)
# Prepare a new one-shot blocking connection
credentials = PlainCredentials(
connection.username, connection.password, erase_on_connect=False)
parameters = ConnectionParameters(
connection.hostname, connection.port, connection.virtual_host,
credentials=credentials, heartbeat=True)
# AMQP-heartbeat timeout must be set manually due to bug in pika 0.9.5:
if parameters.heartbeat:
parameters.heartbeat = timeout
# Create the connection and reset channel
self.connection = BlockingConnection(parameters=parameters)
self.channel = None
def __enter__(self):
# Open and return a new channel
self.channel = self.connection.channel()
return self.channel
def __exit__(self, type, value, traceback):
from pika import spec
self.connection.callbacks.add(
self.channel.channel_number,
spec.Channel.CloseOk,
self.channel.transport._on_rpc_complete
)
self.channel.close()
self.connection.close()
示例11: open_channel
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def open_channel(self, exchange_name, exchange_type):
logger.debug("Connecting to the RabbitMQ server.")
connection = BlockingConnection(parameters=self.connection_parameters)
channel = connection.channel()
try:
channel.exchange_declare(exchange=exchange_name,
exchange_type=exchange_type)
yield channel
except Exception as e:
logger.error(e)
finally:
connection.close()
logger.debug("Connection closed.")
示例12: publish
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def publish(self, topic):
"""Publish the given fact."""
content_len = int(self.headers.getheader('content-length', 0))
fact = self.rfile.read(content_len)
connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'],
port=config['rabbit_port']))
channel = connection.channel()
channel.exchange_declare(exchange=config['exchange'], type='topic')
channel.basic_publish(exchange=config['exchange'],
routing_key=topic,
body=fact)
connection.close()
self.send_response(202)
self.send_header("Content-Type", "text/plain")
self.end_headers()
示例13: get_from_queue
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def get_from_queue(self, queue):
"""Get fact from front of queue."""
connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'],
port=config['rabbit_port']))
channel = connection.channel()
channel.exchange_declare(exchange=config['exchange'], type='topic')
fact = self.wait_on_queue(channel, queue)
connection.close()
if fact is not None:
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
self.wfile.write(fact)
else:
self.send_response(204)
self.send_header("Content-Type", "text/plain")
self.end_headers()
示例14: make_queue
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
def make_queue(self, topic):
"""Create a queue for the given topic."""
connection = BlockingConnection(ConnectionParameters(host=config['rabbit_host'],
port=config['rabbit_port']))
channel = connection.channel()
channel.exchange_declare(exchange=config['exchange'], type='topic')
queue = channel.queue_declare().method.queue
channel.queue_bind(exchange=config['exchange'],
queue=queue,
routing_key=topic)
connection.close()
out = {'queue_name': queue,
'queue_url': QUEUE_URL_TEMPLATE % (config['web_url'], queue,)}
self.send_response(200)
self.send_header("Content-Type", "application/json")
self.end_headers()
dump(out, self.wfile)
示例15: MQConnection
# 需要导入模块: from pika import BlockingConnection [as 别名]
# 或者: from pika.BlockingConnection import close [as 别名]
class MQConnection(object):
def __init__(self):
self.connection = None
self.channel = None
pika_logger = logging.getLogger('pika')
pika_logger.setLevel(logging.CRITICAL)
def connect(self):
self.connection = BlockingConnection(URLParameters(config.MQ_CONFIG))
self.channel = self.connection.channel()
log.info("MQ: Connected")
def is_closed(self):
return self.connection.is_closed
def publish(self, message):
self.channel.basic_publish(
exchange=config.MQ_EXCHANGE,
routing_key=config.MQ_FROM_LISTENER,
body=ujson.dumps(message, ensure_ascii=False),
properties=BasicProperties(
content_type="application/json",
delivery_mode=2))
def consume(self):
return self.channel.consume(config.MQ_TO_LISTENER, no_ack=False, inactivity_timeout=0.1)
def cancel_consumer(self):
self.channel.cancel()
def ack(self, tag):
self.channel.basic_ack(tag)
def nack(self, tag):
self.channel.basic_nack(tag, requeue=False)
def close(self):
self.channel.close()
self.connection.close()
log.info("MQ: Connection closed")