本文整理汇总了Python中pika.adapters.tornado_connection.TornadoConnection类的典型用法代码示例。如果您正苦于以下问题:Python TornadoConnection类的具体用法?Python TornadoConnection怎么用?Python TornadoConnection使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了TornadoConnection类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: PikaClient
class PikaClient(object):
#all the following functions precede in order starting with connect
def connect(self):
try:
logger = logging.getLogger('rmq_tornado')
credentials = pika.PlainCredentials(RMQ_USER, RMQ_PWD)
param = pika.ConnectionParameters(host=RMQ_HOST, port=RMQ_PORT, credentials=credentials)
self.connection = TornadoConnection(param, on_open_callback=self.on_connected)
except Exception as e:
logger.error('Something went wrong... %s', e)
def on_connected(self, connection):
"""When we are completely connected to rabbitmq this is called"""
logger.info('Succesfully connected to rabbitmq')
#open a channel
self.connection.channel(self.on_channel_open)
def on_channel_open(self, new_channel):
"""When the channel is open this is called"""
logging.info('Opening channel to rabbitmq')
global channel
channel = new_channel
示例2: Sender
class Sender(object):
def __init__(self, settings, io_loop):
self.io_loop = io_loop
self.channel = None
self.exchange = "poly"
credentials = None
if settings.get("username", None):
credentials = pika.PlainCredentials(
settings["username"],
settings["password"]
)
self.connection_parameters = None
if credentials or settings.get("host", None) or settings.get("vhost"):
self.connection_parameters = pika.ConnectionParameters(
credentials=credentials,
host=settings.get("host", None),
port=settings.get("port", None),
virtual_host=settings.get("vhost", None)
)
else:
raise Exception("NO self.connection_parameters")
self.settings = settings
def connect(self):
logging.info("MQ connect...")
try:
self.connection = TornadoConnection(
self.connection_parameters,
self.on_connected
)
self.connection.add_on_close_callback(self.on_close)
except socket.error, e:
logging.warn("connection failed, trying again in 5 seconds")
self.io_loop.add_timeout(time.time() + 5, self.connect)
示例3: PikaClient
class PikaClient(object):
def __init__(self, io_loop):
self.io_loop = io_loop
self.connected = False
self.connecting = False
self.connection = None
self.channel = None
self.event_listeners = set([])
def connect(self):
if self.connecting:
return
self.connecting = True
self.connection = TornadoConnection(on_open_callback=self.on_connected)
self.connection.add_on_close_callback(self.on_closed)
def on_connected(self, connection):
self.connected = True
self.connection = connection
self.connection.channel(self.on_channel_open)
def on_channel_open(self, channel):
self.channel = channel
channel.queue_declare(queue="plase", durable=True, callback=self.on_queue_declared)
def on_queue_declared(self, frame):
self.channel.basic_consume(self.on_message, queue="plase")
def on_closed(self, connection):
self.io_loop.stop()
def on_message(self, channel, method, header, body):
self.notify_listeners(body)
def notify_listeners(self, event_obj):
event_json = json.dumps(event_obj)
for listener in self.event_listeners:
listener.write_message(event_json)
def add_event_listener(self, listener):
self.event_listeners.add(listener)
def remove_event_listener(self, listener):
try:
self.event_listeners.remove(listener)
except KeyError:
pass
示例4: AMQPClient
class AMQPClient(object):
channels = {}
def __init__(self, uri, logger, on_connected_callback=None):
self._uri = uri or 'amqp://guest:[email protected]:5672/%2f'
self._logger = logger or logging.getLogger(self.__class__.__name__)
self._on_connected_callback = on_connected_callback
self._amqp_connect()
@property
def uri(self):
return self._uri
@property
def connection(self):
return self._connection
def __getattr__(self, name):
if name in self.channels.keys():
return self.channels[name]
self.channels[name] = AMQPChannelClient(self, name)
return self.channels[name]
def __getitem__(self, name):
return self.__getattr__(name)
def _amqp_connect(self):
self._connection = TornadoConnection(pika.URLParameters(self.uri),
on_open_callback=self._on_amqp_opened, stop_ioloop_on_close=True)
def _on_amqp_opened(self, connection):
self._logger.debug('AMQP connection opened')
self.connection.add_on_close_callback(self._on_connection_closed)
if self._on_connected_callback:
self._on_connected_callback()
def _on_connection_closed(self, connection):
#TODO: Log disconnection details...
#self.log.warning('Server closed connection, reopening: (%s) %s',
#method_frame.method.reply_code,
#method_frame.method.reply_text)
#self.log.debug(connection._is_method_frame())
self._connection = self._amqp_connect()
def close(self):
for channel in self.channels.values():
channel.cancel_consume()
self._logger.debug('Closing AMQP connection')
self._connection.close()
示例5: connect
def connect(self):
if self.connecting:
self.logger.info('django-sockjs-server(PikaClient): Already connecting to RabbitMQ')
return
self.logger.info('django-sockjs-server(PikaClient): Connecting to RabbitMQ')
self.connecting = True
cred = pika.PlainCredentials(self.config.rabbitmq_user, self.config.rabbitmq_password)
param = pika.ConnectionParameters(
host=self.config.rabbitmq_host,
port=self.config.rabbitmq_port,
virtual_host=self.config.rabbitmq_vhost,
credentials=cred
)
try:
self.connection = TornadoConnection(param,
on_open_callback=self.on_connected)
self.connection.add_on_close_callback(self.on_closed)
except AMQPConnectionError:
self.logger.info('django-sockjs-server(PikaClient): error connect, wait 5 sec')
time.sleep(5)
self.reconnect()
self.last_reconnect = now()
示例6: connect
def connect(self):
'Establish RabbitMQ connection.'
self.connection = TornadoConnection(
pika.ConnectionParameters(host=self._host, port=self._port),
on_open_callback=self.on_connected)
logger.info('connecting to RabbitMQ...')
示例7: AMQPClient
class AMQPClient(object):
"Connect to RabbitMQ and create a channel"
def __init__(self, on_msg_callback=None, oid=None, io_loop=None,
on_channel_created=None):
self.oid = oid or options.oid
self.io_loop = io_loop or IOLoop.instance()
self.on_msg_callback = on_msg_callback
self.connection = None
self.channel = None
self._on_channel_created = on_channel_created
self.checker = PeriodicCallback(self._check_connection, 1000)
def connect(self):
"Connect to RabbitMQ"
log.debug("Connecting to RabbitMQ")
if self.connection:
return
self.connection = TornadoConnection(
get_conn_params(),
self.on_connected,
custom_ioloop=self.io_loop)
def on_connected(self, connection):
"Create a channel just after connected"
log.debug("%s is established" % connection)
self.connection.channel(self.on_channel_created)
self.checker.start()
def on_channel_created(self, channel):
"Implement in subclasses"
log.debug("%s is established" % channel)
self.channel = channel
if self._on_channel_created:
self._on_channel_created(channel)
def _check_connection(self):
"Restablish connection to server if we lost it"
if self.connection:
try:
self.connection.socket.fileno()
except socket.error, exc:
log.debug("lost connection to RabbitMQ, %s" % str(exc))
self.checker.stop()
self.connection = None
self.connect()
示例8: connect
def connect(self):
if self.connecting:
return
self.connecting = True
self.connection = TornadoConnection(on_open_callback=self.on_connected)
self.connection.add_on_close_callback(self.on_closed)
示例9: connect
def connect(self):
LOGGER.info('Connecting to RabbitMQ')
# exc_type, exc_value, exc_traceback = sys.exc_info()
# traceback.print_tb(exc_traceback, limit=None, file=sys.stdout)
# self.connecting = True
self._connection = TornadoConnection(rc.connParam,
on_open_callback=self.on_connection_open)
示例10: PikaClient
class PikaClient(object):
"""A modified class as described in pika's demo_tornado.py.
It handles the connection for the Tornado instance. Messaging/RPC
callbacks are handled by the Tornado RequestHandler above."""
def __init__(self):
self.connecting = False
self.connection = None
self.channel = None
# self.L = log_class.Logger()
def connect(self):
if self.connecting:
log.info("Already connecting to RabbitMQ.")
return
# self.L.logger.info("Connecting to RabbitMQ")
self.connecting = True
creds = pika.PlainCredentials("guest", "guest")
params = pika.ConnectionParameters(host="localhost", port=5672, virtual_host="/", credentials=creds)
self.connection = TornadoConnection(params, on_open_callback=self.on_connect)
self.connection.add_on_close_callback(self.on_closed)
def on_connect(self, connection):
self.connection = connection
connection.channel(self.on_channel_open)
def on_channel_open(self, channel):
# self.L.logger.info('Channel Open')
self.channel = channel
# I'm having trouble using named exchanges.
## channel.exchange_declare(exchange='rpc_ex', type='direct',
## auto_delete=True, durable=False,
## callback=self.on_exchange_declare)
def on_exchange_declare(self, frame):
log.info("Exchange declared.")
def on_basic_cancel(self, frame):
log.info("Basic Cancel Ok.")
# If we don't have any more consumer processes running close
self.connection.close()
def on_closed(self, connection):
# We've closed our pika connection so stop the demo
tornado.ioloop.IOLoop.instance().stop()
示例11: PikaClient
class PikaClient(object):
def __init__(self):
self.connected = False
self.connection = None
self.channel = None
self.messages = list()
def connect(self):
self.connection = TornadoConnection(on_open_callback=self.on_connected)
def on_connected(self, connection):
self.connected = True
self.connection = connection
self.connection.channel(self.on_channel_open)
def on_channel_open(self, channel):
pika.log.info("channel open")
self.channel = channel
示例12: connect
def connect(self):
try:
logger = logging.getLogger('rmq_tornado')
credentials = pika.PlainCredentials(RMQ_USER, RMQ_PWD)
param = pika.ConnectionParameters(host=RMQ_HOST, port=RMQ_PORT, credentials=credentials)
self.connection = TornadoConnection(param, on_open_callback=self.on_connected)
except Exception as e:
logger.error('Something went wrong... %s', e)
示例13: connect
def connect(self):
if self.connecting:
return
self.connecting = True
self.connection = TornadoConnection(
pika.ConnectionParameters(host=self.host),
on_open_callback=self.on_connected
)
self.connection.add_on_close_callback(self.on_closed)
示例14: connect
def connect(self):
"Connect to RabbitMQ"
log.debug("Connecting to RabbitMQ")
if self.connection:
return
self.connection = TornadoConnection(
get_conn_params(),
self.on_connected,
custom_ioloop=self.io_loop)
示例15: start
def start(self):
credentials = pika.PlainCredentials('guest', 'guest')
param = pika.ConnectionParameters(host="localhost",
port=5672,
virtual_host="/",
credentials=credentials)
self.connection = TornadoConnection(param, on_open_callback=self.on_connected)
self.connection.set_backpressure_multiplier(100000)