本文整理汇总了Python中pika.BlockingConnection类的典型用法代码示例。如果您正苦于以下问题:Python BlockingConnection类的具体用法?Python BlockingConnection怎么用?Python BlockingConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了BlockingConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: get_url_for_company_name
def get_url_for_company_name(ch, method, properties, body):
""" lookup URL for a company using fortune's profile on the company """
company_data = json.loads(body)
company_name = company_data["name"]
print " [x] Received %r" % (company_name)
# hacks for slugfiying
company_name_slug = company_name.lower().replace("& ", "").replace(".", "").replace(",", "").replace(" ", "-")
company_profile_url = "http://www.inc.com/inc5000/profile/%s" % company_name_slug
r = requests.get(company_profile_url)
soup = BeautifulSoup(r.text)
detail_section = soup.find("div", "inc5000companydata")
try:
company_url = detail_section.find("a")["href"]
except:
company_url = None
company_data["url"] = company_url
to_send = json.dumps(company_data)
ch.basic_ack(delivery_tag = method.delivery_tag)
# now that we've gotten the company's URL, send the data to another worker to get the MG score
connection = BlockingConnection(ConnectionParameters('localhost'))
channel = connection.channel()
channel.exchange_declare(exchange=EXCHANGE_NAME, type='direct')
channel.basic_publish(exchange=EXCHANGE_NAME, routing_key='mg', body=to_send)
示例2: main
def main():
connection = BlockingConnection(ConnectionParameters(host='localhost'))
channel = connection.channel()
channel.queue_declare(queue='calc', durable=True)
print ' [*] Waiting for messages. To exit press CTRL+C'
def callback(ch, method, properties, body):
print " [x] Persistence {}".format(properties.delivery_mode == 2)
print " [x] Received: {}".format(body)
try:
print " [.] Caclulated: {} = {}".format(body, eval(body))
except SyntaxError:
print " [.] Invalid syntax"
print " [x] Done"
ch.basic_ack(delivery_tag=method.delivery_tag)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback,
queue='calc')
try:
channel.start_consuming()
except KeyboardInterrupt:
print "[*] Exit"
return
示例3: Publisher
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()
示例4: Service
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()
示例5: direct_exchange
def direct_exchange():
connection = BlockingConnection(ConnectionParameters("localhost"))
channel = connection.channel()
# connect to exchange and queue
channel.exchange_declare(exchange="direct_logs", type="direct")
# create multiple queues to handle each type of severity
severities = ["critical", "error", "warning", "info", "debug"]
for severity in severities:
channel.queue_declare(queue=severity)
channel.queue_bind(exchange="direct_logs", queue=severity, routing_key=severity)
# pick a random severity level queue to attach this worker to
from random import choice
severities = ["critical", "error", "warning", "info", "debug"]
severity = choice(severities)
severity = "info"
# don't send tasks to this worker until it's ready, keep them in the queue for other workers
# THIS IS IMPORTANT, otherwise adding more workers won't help anything cause
# all msgs will already have been sent to the currently 'busy' worker(s)
channel.basic_qos(prefetch_count=1)
channel.basic_consume(callback, queue=severity)
print " [*] Waiting for messages in %s queue. To exit press CTRL+C" % (severity)
channel.start_consuming()
示例6: Service
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()
示例7: TopicClient
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()
示例8: __init__
def __init__(self, url, context, marshal, unmarshal):
self._context = context
self.marshal, self.unmarshal = marshal, unmarshal
host = match(r"amqp://([\w\d\.]+)", url).group(1)
connection = BlockingConnection(ConnectionParameters(host))
self._channel = connection.channel()
self._channel.exchange_declare(exchange=EXCHANGE, type='topic')
self._init_docstore()
示例9: rabbit_connect
def rabbit_connect(self):
if 'queue_host' in self.config:
host = self.config['queue_host']
else:
host = 'localhost'
conn = BlockingConnection(ConnectionParameters(host, connection_attempts=3))
self.rabbit = conn.channel()
self.rabbit.queue_declare(queue=RABBIT_QUEUE)
示例10: _get_channel
def _get_channel():
global _channel
assert 'kompaq' in settings.INSTALLED_APPS
if not _channel:
connection = BlockingConnection(URLParameters(settings.KOMPAQ_URL))
_channel = connection.channel()
return _channel
示例11: RabbitInput
class RabbitInput(object):
input_name = 'rabbitmq'
def __init__(self, host, port=5672, vhost='/', username='guest', password='guest', queue='default', prefetch=10):
credentials = PlainCredentials(username, password)
self.connection_params = ConnectionParameters(host=host, port=port, virtual_host=vhost, credentials=credentials)
self.queue = queue
self.prefetch = prefetch
def _worker(self, ch, method, properties, body):
# TODO: find out why rabbitmq sucks
if not body:
logger.warning('empty message received from rabbitmq - skipping')
ch.basic_ack(delivery_tag = method.delivery_tag)
return
try:
data = json.loads(body)
except Exception as err:
logger.debug('unable to decode json: %s' % (str(err), ))
else:
for fmt in self.format_modules:
if fmt.bind and self.input_name in fmt.bind:
data = fmt.decode(data)
self.output_threads.write(data)
finally:
ch.basic_ack(delivery_tag = method.delivery_tag)
def handle_input(self):
try:
self.connection = BlockingConnection(self.connection_params)
self.channel = self.connection.channel()
self.channel.queue_declare(queue=self.queue, durable=True)
self.channel.basic_qos(prefetch_count=self.prefetch)
self.channel.basic_consume(self._worker, queue=self.queue)
self.connection.channel()
self.channel.start_consuming()
except (AMQPConnectionError, socket.error) as err:
raise EveConnectionError(err)
def run(self, format_modules, output_modules):
# Start output threads
self.format_modules = format_modules
self.output_modules = output_modules
self.output_threads = OutputThreads(self.output_modules, self.format_modules)
while True:
try:
self.handle_input()
except EveConnectionError as err:
logger.error('connection error in input handler %s: %r - retrying in 1 second' % (self.input_name, err))
sleep(1)
示例12: rabbit_connect
def rabbit_connect(self, callback):
conn = BlockingConnection(ConnectionParameters('localhost'))
self.rabbit = conn.channel()
self.rabbit.queue_declare(queue=RABBIT_QUEUE)
self.rabbit.basic_consume(
callback,
queue=RABBIT_QUEUE,
no_ack=True
)
print "connected"
示例13: post
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()
示例14: work_queue
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()
示例15: Service
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()