class EventDispatcher(object): """Send events as messages. :param connection: Carrot connection. :keyword hostname: Hostname to identify ourselves as, by default uses the hostname returned by :func:`socket.gethostname`. :keyword enabled: Set to ``False`` to not actually publish any events, making :meth:`send` a noop operation. You need to :meth:`close` this after use. """ def __init__(self, connection, hostname=None, enabled=True): self.connection = connection self.hostname = hostname or socket.gethostname() self.enabled = enabled self._lock = threading.Lock() self.publisher = None if self.enabled: self.enable() def enable(self): self.enabled = True self.publisher = EventPublisher(self.connection) def disable(self): self.enabled = False if self.publisher is not None: self.publisher.close() self.publisher = None def send(self, type, **fields): """Send event. :param type: Kind of event. :keyword \*\*fields: Event arguments. """ if not self.enabled: return self._lock.acquire() try: self.publisher.send(Event(type, hostname=self.hostname, **fields)) finally: self._lock.release() def close(self): """Close the event dispatcher.""" self._lock.locked() and self._lock.release() self.publisher and self.publisher.close()
def enable(self): self.enabled = True self.publisher = EventPublisher(self.connection)
class EventDispatcher(object): """Send events as messages. :param connection: Carrot connection. :keyword hostname: Hostname to identify ourselves as, by default uses the hostname returned by :func:`socket.gethostname`. :keyword enabled: Set to ``False`` to not actually publish any events, making :meth:`send` a noop operation. You need to :meth:`close` this after use. """ def __init__(self, connection, hostname=None, enabled=True): self.connection = connection self.hostname = hostname or socket.gethostname() self.enabled = enabled self._lock = threading.Lock() self.publisher = None self._outbound_buffer = deque() if self.enabled: self.enable() def enable(self): self.enabled = True self.publisher = EventPublisher(self.connection) def disable(self): self.enabled = False if self.publisher is not None: self.publisher.close() self.publisher = None def send(self, type, **fields): """Send event. :param type: Kind of event. :keyword \*\*fields: Event arguments. """ if not self.enabled: return self._lock.acquire() event = Event(type, hostname=self.hostname, **fields) try: try: self.publisher.send(event) except Exception, exc: self._outbound_buffer.append((event, exc)) finally: self._lock.release() def flush(self): while self._outbound_buffer: event, _ = self._outbound_buffer.popleft() self.publisher.send(event) def close(self): """Close the event dispatcher.""" self._lock.locked() and self._lock.release() self.publisher and self.publisher.close()
def __init__(self, connection, hostname=None, enabled=True): self.connection = connection self.publisher = EventPublisher(self.connection) self.hostname = hostname or socket.gethostname() self.enabled = enabled self._lock = threading.Lock()