本文整理汇总了Python中pika.adapters.SelectConnection.channel方法的典型用法代码示例。如果您正苦于以下问题:Python SelectConnection.channel方法的具体用法?Python SelectConnection.channel怎么用?Python SelectConnection.channel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pika.adapters.SelectConnection
的用法示例。
在下文中一共展示了SelectConnection.channel方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import channel [as 别名]
class dataChannel:
""" The dataChannel is the base class of all our datasource.
It's purpose is to: a). Setup the queues"""
def __init__(self,ds_name):
self.channel = None
self.dc_id = "eek"#dc_id
## query mongoDB to find all the particulars about this
## data channel including: which queue is listening to,
## which exchange, the routing key..etc.
self.ret_queue = "ret_queue"
self.connection = None
self.channel = None
self.connected = False;
self.connecting = False;
self.exchange = "test_x";
self.queue = "test_q"
self.routing_key = "test_q"
## use the ds to the find which exchange and which queue this
## datachannel listens
def mongo_db(self):
## connect to the mongodb
mongo_conn = Connection('localhost',27017)
db = mongo_conn['data_channels']
coll= db['bbox_pts']
def connect(self):
print self
if self.connecting:
print ('1...PikaClient: Already connecting to RabbitMQ')
return
print ('1...PikaClient: Connecting to RabbitMQ on localhost:5672')
self.connecting = True
credentials = pika.PlainCredentials('guest', 'guest')
param = pika.ConnectionParameters(host='localhost',
port=5672,
virtual_host="/",
credentials=credentials)
host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
self.connection = SelectConnection(ConnectionParameters(host),
self.on_connected)
if self.connection != None:
print self.connection
print 'connection'
def on_connected(self,connection):
print '2...PikaClient: Connected to RabbitMQ on localhost:5672'
self.connection = connection
self.connection.channel(self.on_channel_open)
self.connected = True
def on_channel_open(self, channel):
print ('3...PikaClient: Channel Open, Declaring Exchange')
self.channel = channel
self.channel.exchange_declare(exchange=self.exchange,
type="direct",
auto_delete=False,
durable=True,
callback=self.on_exchange_declared)
def on_exchange_declared(self, frame):
print ('4...PikaClient: Exchange Declared, Declaring Queue')
self.channel.queue_declare(queue=self.queue,
auto_delete=False,
durable=True,
exclusive=False,
callback=self.on_queue_declared)
def on_queue_declared(self, frame):
print('5...PikaClient: Queue Declared, Binding Queue')
print "demo_receive: Queue Declared"
# self.channel.basic_consume(self.handle_delivery, queue='test_q')
self.channel.queue_bind(exchange=self.exchange,
queue=self.queue,
routing_key=self.routing_key,
callback=self.on_queue_bound)
def on_queue_bound(self, frame):
print('6...PikaClient: Queue Bound, Issuing Basic Consume')
self.channel.basic_consume(consumer_callback=self.handle_delivery,
queue=self.queue)
def handle_delivery(self,channel, method_frame, header_frame, body):
print "7...Basic.Deliver %s delivery-tag %i: %s" %\
(header_frame.content_type,
method_frame.delivery_tag,
body)
print body
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
def get_data(self,args):
print "Please implement get_data"
示例2: dataChannel
# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import channel [as 别名]
class dataChannel(object):
""" The dataChannel is the base class of all our datasource.
It's purpose is to: a). Setup the queues"""
def __init__(self, server_name='test', mq_exchange='', mq_queue = '',mq_host=''):
self.channel = None
self.id = server_name
self.queue_counter = 0
self.queue =mq_queue
self.routing_key = ''
self.exchange = mq_exchange
self.connection = None
self.connected = False
self.connecting = False
self.rabbithost = mq_host
logging.getLogger('pika').setLevel(logging.DEBUG)
def get_connection(self):
return self.connection
def connect(self):
if self.connecting:
return
self.connecting = True
credentials = pika.PlainCredentials('guest', 'guest')
params = pika.ConnectionParameters(host="hackinista.com",
port=5672,
virtual_host="/",
credentials=credentials)
host = (len(sys.argv) > 1) and sys.argv[1] or '127.0.0.1'
try:
self.connection = SelectConnection(params, self.on_connected)
except:
# self.L.critical("Error connecting to rabbitmq on host =
# "+self.host);
sys.exit(-1)
###
def on_connected(self, connection):
self.connection = connection
self.connection.channel(self.on_channel_open)
self.connected = True
def on_channel_open(self, channel):
self.channel = channel
try:
self.channel.queue_declare(queue=self.queue,
auto_delete=False,
durable=True,
exclusive=False,
callback=self.on_queue_declared)
except:
print "Error declaring queue = " + self.queue
sys.exit(-1)
def on_queue_declared(self, frame):
try:
self.channel.queue_bind(exchange=self.exchange,
queue=self.queue,
routing_key=self.routing_key,
callback=self.on_queue_bound)
except:
print "Binding to queue = " + self.queue
pass
def on_queue_bound(self, frame):
self.channel.basic_consume(consumer_callback=self.handle_delivery,
queue=self.queue, no_ack=False)
def handle_delivery(self, channel, method_frame, header_frame, body):
print "7...Basic.Deliver %s delivery-tag %i: %s" %\
(header_frame.content_type,
method_frame.delivery_tag,
body)
self.data_op(body)
channel.basic_ack(delivery_tag=method_frame.delivery_tag)
def data_op(self, args):
print "Please implement get_data"
示例3: Amqp
# 需要导入模块: from pika.adapters import SelectConnection [as 别名]
# 或者: from pika.adapters.SelectConnection import channel [as 别名]
class Amqp(object):
def __init__(self, conf):
# RabbitMQ general options
self.cacertfile = conf['cacertfile']
self.certfile = conf['certfile']
self.exchange = conf['exchange']
self.status_exchange = conf['status_exchange']
self.fail_if_no_peer_cert = conf['fail_if_no_peer_cert']
self.heartbeat = conf['heartbeat']
self.host = conf['host']
self.keyfile = conf['keyfile']
self.password = conf['password']
self.port = conf['port']
self.ssl_port = conf['ssl_port']
self.queue = conf['uuid']
self.retry_timeout = conf['retry_timeout']
self.ssl_auth = conf['ssl_auth']
self.use_ssl = conf['use_ssl']
self.username = conf['username']
self.vhost = conf['vhost']
# Connection and channel initialization
self.connection = None
self.channel = None
# Plain credentials
credentials = PlainCredentials(self.username, self.password)
pika_options = {'host': self.host,
'port': self.port,
'virtual_host': self.vhost,
'credentials': credentials}
# SSL options
if self.use_ssl:
pika_options['ssl'] = True
pika_options['port'] = self.ssl_port
if self.ssl_auth:
pika_options['credentials'] = ExternalCredentials()
pika_options['ssl_options'] = {
'ca_certs': self.cacertfile,
'certfile': self.certfile,
'keyfile': self.keyfile,
'cert_reqs': CERT_REQUIRED
}
if self.heartbeat:
pika_options['heartbeat'] = self.heartbeat
self.parameters = None
try:
self.parameters = pika.ConnectionParameters(**pika_options)
except TypeError as err:
self.logger.debug(err)
# Let's be compatible with original pika version (no integer for
# heartbeats and no ssl.
self.logger.warning("Wrong pika lib version, won't use ssl.")
pika_options['heartbeat'] = True
if self.use_ssl:
self.use_ssl = False
pika_options['port'] = self.port
del pika_options['ssl']
if self.ssl_auth:
self.ssl_auth = False
del pika_options['ssl_options']
self.parameters = pika.ConnectionParameters(**pika_options)
def connect(self):
SelectPoller.TIMEOUT = .1
self.connection = SelectConnection(self.parameters, self.on_connected)
self.connection.ioloop.start()
def close(self, amqperror=False):
if (self.connection and not self.connection.closing
and not self.connection.closed):
self.logger.debug("Closing connection")
self.connection.close()
#self.connection.ioloop.start()
def on_remote_close(self, code, text):
self.logger.debug("Remote channel close, code %d" % code)
time.sleep(2)
if code != 200:
self.close()
raise AmqpError(text)
def on_connection_closed(self, frame):
self.connection.ioloop.stop()
def on_connected(self, connection):
self.connection = connection
self.connection.add_on_close_callback(self.on_connection_closed)
self.connection.channel(self.on_channel_open)