当前位置: 首页>>代码示例>>Python>>正文


Python Connection.read_frames方法代码示例

本文整理汇总了Python中haigha.connection.Connection.read_frames方法的典型用法代码示例。如果您正苦于以下问题:Python Connection.read_frames方法的具体用法?Python Connection.read_frames怎么用?Python Connection.read_frames使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在haigha.connection.Connection的用法示例。


在下文中一共展示了Connection.read_frames方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: FibonacciRpcClient

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]
class FibonacciRpcClient(object):
    def __init__(self):
        self.connection = Connection(host='localhost',
                                     heartbeat=None, debug=True)

        self.channel = self.connection.channel()

        result = self.channel.queue.declare(exclusive=True)
        self.callback_queue = result[0]
        print("callback_queue:", self.callback_queue)

        self.channel.basic.consume(self.callback_queue,
                                   self.on_response, no_ack=True)

    def on_response(self, msg):
        if msg.properties["correlation_id"] == self.corr_id:
            self.response = msg.body

    def call(self, n):
        self.response = None
        self.corr_id = str(uuid.uuid4())
        msg = Message(str(n), reply_to=self.callback_queue,
                      correlation_id=self.corr_id)
        self.channel.basic.publish(msg, '', 'rpc_queue')

        while self.response is None:
            self.connection.read_frames()

        return int(self.response)
开发者ID:dehun,项目名称:haigha,代码行数:31,代码来源:rpc_client.py

示例2: AsyncClient

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]
class AsyncClient(object):
    __metaclass__ = ClientMeta

    def start(self, **kwargs):
        self.connection = Connection(**kwargs)
        self.channel = self.connection.channel()
        self.loop = TaskLoop()
        self.insert_task(self.read_frames, interval=0.01)
        self.insert_task(self.__run)
        self.loop.start()

    def __run(self):
        for declaration in itertools.chain(self._exchanges, self._queues, self._consumers):
            declaration.client = weakref.ref(self)
            declaration.declare()

        # start 'auto' tasks
        for name, attr in self.__class__.__dict__.iteritems():
            if isinstance(attr, Task) and attr.auto:
                getattr(self, name)()

        for task, args, kwargs in self._tasks:
            print 'scheduling %s from _tasks' % task
            self.insert_task(task, *args, **kwargs)

        # if there's a run function which isn't already a task, queue it
        class_run = getattr(self.__class__, 'run', None)
        self_run = getattr(self, 'run', None)
        if callable(self_run) and not isinstance(class_run, Task):
            self.run()
            self.stop()

    def stop(self):
        self.connection.close()

    def insert_task(self, task, *args, **kwargs):
        loop = getattr(self, 'loop', None)
        if isinstance(loop, TaskLoop):
            loop.insert_task(task, *args, **kwargs)
        else:
            self._tasks.append((task, args, kwargs))

    def read_frames(self):
        if self.connection.close_info:
            self.loop.stop()
        else:
            self.connection.read_frames()

    @task
    def basic_qos(self, **kwargs):
        self.channel.basic.qos(**kwargs)
开发者ID:altaurog,项目名称:poqa,代码行数:53,代码来源:client.py

示例3: AppClient

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]

#.........这里部分代码省略.........
				context:
				{
					operation:<operation>,
					params:<params>,
					cid:<id for the request>
				},
				content:<content>
			}
		'''
		logging.debug(" Reply: Getting msg: {0}".format(message))

		body = str(message.body).encode('string_escape')
		if not body:
			pass

		body=json.loads(body)

		context = body["context"] or None
		if not context:
			logging.debug("QueueServer:Received invalid message: No context: {0}".format(message))		
			return 

		cid = context["cid"]

		callback = self._pending_requests[cid]
		if not callback:
			logging.error("No callback registered for cid:",cid)
			return 

		gevent.spawn(callback, body["content"])
		gevent.sleep(0)	

	def send(self, message, options, callback):
		
		context = options or {}

		context["operation"]="get"
		context["cid"]=1

		self._pending_requests[context["cid"]]=callback

		message = Message(json.dumps({
			"context": context,
			"content" : message
			}),None, reply_to=self.reply_queue_name)

		logging.debug("Sending message to {0}|{1}".format(self.exchange,self.queueName, message))
		self._channel.basic.publish( message, self.exchange,self.queueName)

		gevent.sleep(0)
		

	def start(self):
		self._connection = Connection(
			transport='gevent',
  			user='guest', password='guest',
  			vhost=self.vhost, host=self.queueServer,
  			heartbeat=None, debug=True)
		self._channel = self._connection.channel()
		self._channel.add_close_listener(self._channel_closed_cb)
    	
	    # Create and configure message exchange and queue
		self._channel.exchange.declare(self.exchange, 'topic')
		self._channel.queue.declare(self.queueName, auto_delete=False)		
		self._channel.queue.declare(self.reply_queue_name, auto_delete=False)

		print "reply q name", self.reply_queue_name
		self._channel.queue.bind(self.reply_queue_name, self.exchange, self.reply_queue_name)
		# self._channel.basic.consume(queue=self.reply_queue_name,
		#                             consumer=self._process_reply)

		# Start message pump
		self.running = True
		gevent.spawn(self._message_pump_greenthread)
		print "Started"
		gevent.sleep(0)
		

	def _message_pump_greenthread(self):
	    logging.debug("Entering Message Pump")
	    try:
	      while self.running:
	        # Pump
	        self._connection.read_frames()
	        
	        # Yield to other greenlets so they don't starve
	        gevent.sleep()
	    except Exception as e:
	    	print e
	    finally:
	      logging.debug("Leaving Message Pump, {0}".format(self.running))	      
	    return


	def stop(self):
		self.running = False


	def _channel_closed_cb(self,*args):
		print "Channel Closed"
开发者ID:polavishnu4444,项目名称:RestApiPerfTester,代码行数:104,代码来源:QueueClientBase.py

示例4: on_request

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]
    tags=predict.InputImagePredict(filename,SETTINGS_FILE_TAGS,"tags",classifierTags)

    redisDb.insertKey(imageHash+"_tags",tags)
    # db.InsertData({'imageName':imageHash,'similarProducts':[],'crossProducts':[],'tags':tags},mode="tags")
    
    return {'imageURL':imageURL,'result':tags}

def on_request(msg):
    val = json.loads(str(msg.body))
    print val
    imageURL=val['url']
    if val['type']=="similar":
	result=computeNN(imageURL)
    else:
	result=computeTags(imageURL)
    print result
    reply_to = msg.properties["reply_to"]
    correlation_id = msg.properties["correlation_id"]
    resp = Message(json.dumps(result), correlation_id=correlation_id)
    channel.basic.publish(resp,'',reply_to)
    delivery_info = msg.delivery_info
    channel.basic.ack(delivery_info["delivery_tag"])

channel.basic.qos(prefetch_count=2)
channel.basic.consume('rpc_queue', on_request, no_ack=False)

print " [x] Awaiting RPC requests"
while not channel.closed:
    connection.read_frames()
开发者ID:DeepFashion,项目名称:deepFashion,代码行数:31,代码来源:exp.py

示例5: Connection

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]
class Connection(object):
    def __init__(self, host, port, virtualhost, user, password, queue):
        self.conn_id = hex(int(random.random() * 2**32))

        self._conn_kwargs = {
            "host": host,
            "port": port,
            "vhost": virtualhost,
            "user": user,
            "password": password,
            "transport": "gevent",
            "close_cb": self._on_disconnect,
            "logger": logger,
        }
        self.queue = queue

        self._connection = None
        self._channel = None
        self.lock = Semaphore()
        self.broken = False

    def log_debug(self, msg, *args, **kwargs):
        # Ghetto log handler
        logger.debug("[Conn {0}] {1}".format(self.conn_id, msg), *args, **kwargs)

    def log_exception(self, msg, *args, **kwargs):
        logger.exception("[Conn {0}] {1}".format(self.conn_id, msg), *args, **kwargs)

    def disconnect(self):
        self.log_debug("Disconnecting")
        self.broken = True
        self._connection.disconnect()

    def _on_disconnect(self):
        self.log_debug("Received disconnect")
        self.broken = True

    def _on_channel_closed(self, channel):
        # Scrape the connection if our channel is closed.
        self.log_debug("Received channel close")
        self.disconnect()

    def _open_connection(self):
        self.log_debug("Opening RabbitMQ connection")
        self._connection = HaighaConnection(**self._conn_kwargs)
        self._start_read_loop()

    def _open_channel(self):
        # Open a channel and make sure we know if it gets closed
        self.log_debug("Opening RabbitMQ channel")
        self._channel = self._connection.channel()
        self._channel.add_close_listener(self._on_channel_closed)
        self._channel.queue.declare(self.queue, auto_delete=True)

    def _ensure_open(self):
        if self._channel is None:
            self._open_connection()
            self._open_channel()

    @exec_or_break
    def _read_frames(self):
        self._connection.read_frames()

    def _read_loop(self):
    # The message pump needs to run for the connection to actually do something.
        while not self.broken:
            self._read_frames()
            gevent.sleep()  # Yield to other greenlets so they don't starve

    def _start_read_loop(self):
        self.log_debug("Starting connection loop")
        gevent.spawn(self._read_loop)  # Power our connection

    @exec_or_break
    def dispatch(self, key):
        self._ensure_open()
        self._channel.basic.publish(Message(key), "", self.queue)

    @exec_or_break
    def consume(self, consumer):
        # Start consuming messages from the queue (they will be passed to `consumer`)
        def cb():
            self.log_debug("Registered as consumer")

        def consumer_wrapper(message):
            self.log_debug("Received a message: %s", message.body)
            consumer(message)

        self._ensure_open()
        self._channel.basic.consume(queue=self.queue, consumer=consumer_wrapper, cb=cb)
开发者ID:krallin,项目名称:tutorial-workertier,代码行数:92,代码来源:rabbitmq.py

示例6: RabbitConnexion

# 需要导入模块: from haigha.connection import Connection [as 别名]
# 或者: from haigha.connection.Connection import read_frames [as 别名]

#.........这里部分代码省略.........
        if config.RABBITMQ_LOOPBACK:
            self.logger.warning("Looopback mode: No connection, waiting for ever...")
            while True:
                time.sleep(1)

        while True:
            try:
                time.sleep(1)
                self.logger.debug("Connecting to RabbitMQ (user=%s,host=%s,port=%s,vhost=%s)" % (
                    config.RABBITMQ_USER, config.RABBITMQ_HOST, config.RABBITMQ_PORT, config.RABBITMQ_VHOST))
                self.cox = Connection(user=config.RABBITMQ_USER, password=config.RABBITMQ_PASSWORD,
                                      vhost=config.RABBITMQ_VHOST, host=config.RABBITMQ_HOST, port=config.RABBITMQ_PORT,
                                      debug=config.RABBITMQ_DEBUG)

                self.logger.debug("Creating the channel")
                self.ch = self.cox.channel()

                # Name will come from a callback
                global queue_name
                queue_name = None

                def queue_qb(queue, msg_count, consumer_count):
                    self.logger.debug("Created queue %s" % (queue,))
                    global queue_name
                    queue_name = queue

                self.logger.debug("Creating the queue")
                if not self.watchdog:
                    # 'Normal', tempory queue
                    self.ch.queue.declare(auto_delete=True, nowait=False, cb=queue_qb)
                else:
                    # Persistant queue, if not in test mode
                    if len(sys.argv) > 1 and sys.argv[1] == '--test':
                        self.ch.queue.declare(config.FB_QUEUE, auto_delete=True, nowait=False, cb=queue_qb)
                    else:
                        self.ch.queue.declare(config.FB_QUEUE, auto_delete=False, nowait=False, cb=queue_qb)

                for i in range(0, 10):  # Max 10 seconds
                    if queue_name is None:
                        time.sleep(1)

                if queue_name is None:
                    self.logger.warning("Queue creation timeout !")
                    raise Exception("Cannot create queue !")

                self.logger.debug("Binding the exchange %s" % (config.RABBITMQ_EXCHANGE,))
                self.ch.queue.bind(queue_name, config.RABBITMQ_EXCHANGE, '')

                self.logger.debug("Binding the comsumer")
                self.ch.basic.consume(queue_name, self.consumer)

                self.logger.debug("Ready, waiting for events !")
                while True:
                    if not hasattr(self.ch, 'channel') or (
                            hasattr(self.ch.channel, '_closed') and self.ch.channel._closed):
                        self.logger.warning("Channel is closed")
                        raise Exception("Connexion or channel closed !")
                    self.cox.read_frames()

            except Exception as e:
                self.logger.error("Error in run: %s" % (e,))
            finally:
                if self.cox is not None:
                    self.cox.close()

    def send_message(self, headers, message):
        """Send a message to the queue"""

        # Append current ts
        headers['when'] = str(int(time.time()))

        self.logger.info("Sending message (with headers %s) %s to %s" % (headers, message, config.RABBITMQ_EXCHANGE))

        if config.RABBITMQ_LOOPBACK:
            self.logger.info("Sending using loopback, calling function directly")

            class FalseMsg():
                def __init__(self, body, headers):
                    self.body = body
                    self.properties = {'application_headers': headers}

            self.consumer(FalseMsg(message, headers))

        else:
            self.ch.basic.publish(Message(message, application_headers=headers), config.RABBITMQ_EXCHANGE, '')

    def add_stomp_server(self, s):
        """Handle a new stomp server"""
        self.stompservers.append(s)
        self.update_stats()

    def remove_stomp_server(self, s):
        """Stop handeling a stomp server"""
        self.stompservers.remove(s)
        self.update_stats()

    def update_stats(self):
        """Update stats"""
        # self.gauge.send(config.STATS_GAUGE_NB_CLIENTS, len(self.stompservers))
        pass
开发者ID:ebu,项目名称:radiodns-plugit,代码行数:104,代码来源:rabbitmq.py


注:本文中的haigha.connection.Connection.read_frames方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。