def _dispatch_fd_events(self, fd_event_map): """ Helper to dispatch callbacks for file descriptors that received events. Before doing so we re-calculate the event mask based on what is currently set in case it has been changed under our feet by a previous callback. We also take a store a refernce to the fd_event_map so that we can detect removal of an fileno during processing of another callback and not generate spurious callbacks on it. :param dict fd_event_map: Map of fds to events received on them. """ # Reset the prior map; if the call is nested, this will suppress the # remaining dispatch in the earlier call. self._processing_fd_event_map.clear() self._processing_fd_event_map = fd_event_map for fileno in dictkeys(fd_event_map): if fileno not in fd_event_map: # the fileno has been removed from the map under our feet. continue events = fd_event_map[fileno] for evt in [READ, WRITE, ERROR]: if fileno not in self._fd_events[evt]: events &= ~evt if events: handler = self._fd_handlers[fileno] handler(fileno, events)
def close(self, reply_code=0, reply_text="Normal Shutdown"): """Will invoke a clean shutdown of the channel with the AMQP Broker. :param int reply_code: The reply code to close the channel with :param str reply_text: The reply text to close the channel with """ LOGGER.info('Channel.close(%s, %s)', reply_code, reply_text) if not self.is_open: raise exceptions.ChannelClosed() # Cancel the generator if it's running if self._generator: self.cancel() # If there are any consumers, cancel them as well if self._consumers: LOGGER.debug('Cancelling %i consumers', len(self._consumers)) for consumer_tag in dictkeys(self._consumers): self.basic_cancel(consumer_tag=consumer_tag) self._set_state(self.CLOSING) self._rpc(spec.Channel.Close(reply_code, reply_text, 0, 0), None, [spec.Channel.CloseOk]) self._set_state(self.CLOSED) self._cleanup()
def consumer_tags(self): """Property method that returns a list of currently active consumers :rtype: list """ return dictkeys(self._consumers)
def _process_fd_events(self, fd_event_map, write_only): """ Processes the callbacks for each fileno we've recieved events. Before doing so we re-calculate the event mask based on what is currently set in case it has been changed under our feet by a previous callback. We also take a store a refernce to the fd_event_map in the class so that we can detect removal of an fileno during processing of another callback and not generate spurious callbacks on it. :param dict fd_event_map: Map of fds to events recieved on them. """ self._processing_fd_event_map = fd_event_map for fileno in dictkeys(fd_event_map): if fileno not in fd_event_map: # the fileno has been removed from the map under our feet. continue events = fd_event_map[fileno] for ev in [READ, WRITE, ERROR]: if fileno not in self._fd_events[ev]: events &= ~ev if events: handler = self._fd_handlers[fileno] handler(fileno, events, write_only=write_only)
def process_timeouts(self): """Process the self._timeouts event stack""" start_time = time.time() for timeout_id in dictkeys(self._timeouts): if self._timeouts[timeout_id]['deadline'] <= start_time: callback = self._timeouts[timeout_id]['callback'] del self._timeouts[timeout_id] callback()
def stop_consuming(self, consumer_tag=None): """Sends off the Basic.Cancel to let RabbitMQ know to stop consuming and sets our internal state to exit out of the basic_consume. """ if consumer_tag: self.basic_cancel(consumer_tag) else: for consumer_tag in dictkeys(self._consumers): self.basic_cancel(consumer_tag) self.wait = True
def close(self, reply_code=0, reply_text="Normal Shutdown"): if self.is_closed: raise exceptions.ChannelClosed('Already closed: %s' % self) LOGGER.info('Closing channel (%s): %r on %s', reply_code, reply_text, self) for consumer_tag in dictkeys(self._consumers): self.basic_cancel(consumer_tag=consumer_tag) self.is_closed = True return self.rpc(spec.Channel.Close(reply_code, reply_text, 0, 0), [spec.Channel.CloseOk])
def close(self, reply_code=0, reply_text="Normal Shutdown"): """Will invoke a clean shutdown of the channel with the AMQP Broker. :param int reply_code: The reply code to close the channel with :param str reply_text: The reply text to close the channel with """ if not self.is_open: raise exceptions.ChannelClosed() LOGGER.info('Channel.close(%s, %s)', reply_code, reply_text) if self._consumers: LOGGER.debug('Cancelling %i consumers', len(self._consumers)) for consumer_tag in dictkeys(self._consumers): self.basic_cancel(consumer_tag=consumer_tag) self._set_state(self.CLOSING) self._rpc(spec.Channel.Close(reply_code, reply_text, 0, 0), self._on_closeok, [spec.Channel.CloseOk])
def process_timeouts(self): """Process the self._timeouts event stack""" for timeout_id in dictkeys(self._timeouts): if self._deadline_passed(timeout_id): self._call_timeout_method(self._timeouts.pop(timeout_id))
def consumer_tags(self): return dictkeys(self._consumers)