def connect(self):
"""This method connects to RabbitMQ via the Torando Connectoin Adapter, returning the
connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
:rtype: pika.SelectConnection
"""
pi('connect')
if self._connecting:
print 'RabbitMQClient: Already connecting to RabbitMQ'
return
print 'RabbitMQClient: Connecting to RabbitMQ on localhost:5672, Object: %s' % (self,)
self._connecting = True
pp(self, 'CONNECT')
return pika.adapters.TornadoConnection(parameters=self._parameters,
on_open_callback=self.on_connection_opened,
stop_ioloop_on_close=False)
python类SelectConnection()的实例源码
def connect(self):
"""This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
:rtype: pika.SelectConnection
"""
log.debug('Connecting to %s', self._url)
return pika.SelectConnection(pika.URLParameters(self._url),
self.on_connection_open,
stop_ioloop_on_close=False)
def on_connection_open(self, unused_connection):
"""This method is called by pika once the connection to RabbitMQ has
been established. It passes the handle to the connection object in
case we need it, but in this case, we'll just mark it unused.
:type unused_connection: pika.SelectConnection
"""
log.debug('Connection opened')
self.add_on_connection_close_callback()
self.open_channel()
def run(self):
"""Run the example consumer by connecting to RabbitMQ and then
starting the IOLoop to block and allow the SelectConnection to operate.
"""
self._connection = self.connect()
self._connection.ioloop.start()
def from_settings(settings):
"""
:param: settings object
:return: Channel object
"""
connection_type = settings.get('RABBITMQ_CONNECTION_TYPE', RABBITMQ_CONNECTION_TYPE)
connection_parameters = settings.get('RABBITMQ_CONNECTION_PARAMETERS', RABBITMQ_CONNECTION_PARAMETERS)
connection = {
'blocking': pika.BlockingConnection,
'libev': pika.LibevConnection,
'select': pika.SelectConnection,
'tornado': pika.TornadoConnection,
'twisted': pika.TwistedConnection
}[connection_type](pika.ConnectionParameters(**connection_parameters))
channel = connection.channel()
channel.basic_qos(prefetch_count=1)
url = settings.get('REDIS_URL', REDIS_URL)
host = settings.get('REDIS_HOST', REDIS_HOST)
port = settings.get('REDIS_PORT', REDIS_PORT)
# REDIS_URL takes precedence over host/port specification.
if url:
redis_server = redis.from_url(url)
else:
redis_server = redis.Redis(host=host, port=port)
return channel, redis_server
def rabbitmq_connect(self):
"""This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika. If you want the reconnection to work, make
sure you set stop_ioloop_on_close to False, which is not the default
behavior of this adapter.
:rtype: pika.SelectConnection
"""
logger.info('Connecting to %s', self._url)
return pika.SelectConnection(pika.URLParameters(self._url),
self.on_connection_open,
stop_ioloop_on_close=False)
def on_connection_open(self, unused_connection):
"""This method is called by pika once the connection to RabbitMQ has
been established. It passes the handle to the connection object in
case we need it, but in this case, we'll just mark it unused.
:type unused_connection: pika.SelectConnection
"""
logger.info('Connection opened')
self.add_on_connection_close_callback()
self.open_channel()
def connect(self):
return pika.SelectConnection(pika.URLParameters(self._url),
self._on_connection_open,
stop_ioloop_on_close=False)
def connect(self):
LOGGER.info('Connecting to %s', self._url)
return pika.SelectConnection(pika.URLParameters(self._url),
self.on_connection_open,
stop_ioloop_on_close=False)
def connect(self):
if not self._connection or self._connection.is_closed:
LOGGER.info('Connecting to %s', self._url)
self._connection = pika.BlockingConnection(pika.URLParameters(self._url))
return pika.SelectConnection(pika.URLParameters(self._url),
on_open_callback=self.on_connection_open,
on_close_callback=self.on_connection_closed,
stop_ioloop_on_close=False)
def connect(self):
"""This method connects to RabbitMQ, returning the connection handle.
When the connection is established, the on_connection_open method
will be invoked by pika.
:rtype: pika.SelectConnection
"""
log.info('Connecting to %s', self.amqp_url)
return adapters.TornadoConnection(pika.URLParameters(self.amqp_url),
self.on_connection_open)
def on_connection_open(self, unused_connection):
"""This method is called by pika once the connection to RabbitMQ has
been established. It passes the handle to the connection object in
case we need it, but in this case, we'll just mark it unused.
:type unused_connection: pika.SelectConnection
"""
log.info('Connection opened')
self.add_on_connection_close_callback()
self.open_channel()
def run(self):
"""Run the consumer by connecting to RabbitMQ and then
starting the IOLoop to block and allow the SelectConnection to operate.
"""
self._connection = self.connect()
# self._connection.ioloop.start()
connection.py 文件源码
项目:Distributed-Multi-User-Scrapy-System-with-a-Web-UI
作者: aaldaber
项目源码
文件源码
阅读 21
收藏 0
点赞 0
评论 0
def from_settings(settings, spider_name):
connection_type = settings.get('RABBITMQ_CONNECTION_TYPE',
RABBITMQ_CONNECTION_TYPE)
queue_name = "%s:requests" % spider_name
connection_host = settings.get('RABBITMQ_HOST')
connection_port = settings.get('RABBITMQ_PORT')
connection_username = settings.get('RABBITMQ_USERNAME')
connection_pass = settings.get('RABBITMQ_PASSWORD')
connection_attempts = 5
retry_delay = 3
credentials = pika.PlainCredentials(connection_username, connection_pass)
connection = {
'blocking': pika.BlockingConnection,
'libev': pika.LibevConnection,
'select': pika.SelectConnection,
'tornado': pika.TornadoConnection,
'twisted': pika.TwistedConnection
}[connection_type](pika.ConnectionParameters(host=connection_host,
port=connection_port, virtual_host='/',
credentials=credentials,
connection_attempts=connection_attempts,
retry_delay=retry_delay))
channel = connection.channel()
channel.queue_declare(queue=queue_name, durable=True)
return channel
def start(self):
'''Start all of the exciting AMQPness.'''
# Connect to RabbitMQ
parameters = pika.URLParameters(self.url)
connection = pika.SelectConnection(parameters, self.on_connected)
# Main loop:
try:
connection.ioloop.start()
except KeyboardInterrupt:
# shut down gracefully
connection.close()
connection.ioloop.start()
def start(self):
"""Run the example consumer by connecting to RabbitMQ and then
starting the IOLoop to block and allow the SelectConnection to operate.
"""
pi('start')
pc('self.connect')
self._connection = self.connect()
ps('self.connect')
# self._connection.ioloop.start()
pp(self, 'start')
pr('start')
def connect(self):
LOGGER.info('Connecting to %s', self._url)
return pika.SelectConnection(pika.URLParameters(self._url),
self.on_connection_open,
stop_ioloop_on_close=False)
def _init_messaging(self):
LOG.info('Initializing connection to rabbitmq node..')
#construct credentials
credentials = pika_credentials.PlainCredentials(
username=self.rabbitUID,
password=self.rabbitPass
)
parameters = pika.ConnectionParameters(
host=self.rabbitHost,
port=self.rabbitPort,
virtual_host=self.rabbitVHost,
credentials=credentials,
channel_max=self.channel_max,
frame_max=self.frame_max,
heartbeat_interval=self.heartbeat_rate,
connection_attempts=self.connection_attemps,
retry_delay=self.retry_delay,
socket_timeout=self.socket_timeout,
locale=self.pika_locale
)
self.connection = pika.BlockingConnection(parameters=parameters)
#self.connection = pika.SelectConnection(parameters=parameters, on_open_callback=on_open)
self.channel = self.connection.channel()
# JFP 27/02/2017
# self.channel.confirm_delivery()
# JFP 27/02/2017
# self.channel.basic_qos(prefetch_size=0,
# prefetch_count=0,
# all_channels=False
# )
self.channel.exchange_declare(exchange=self.exchange_name,
exchange_type=self.exchange_type,
passive=self.is_passive,
durable=self.is_durable,
auto_delete=self.is_auto_delete,
internal=self.is_internal,
arguments=self.arguments
)