class _PullToPushProducer: """A push producer that wraps a pull producer.""" def __init__( self, reactor: IReactorTime, producer: IPullProducer, consumer: IConsumer ): self._clock = Clock(reactor) self._producer = producer self._consumer = consumer # While running we use a looping call with a zero delay to call # resumeProducing on given producer. self._looping_call = None # type: Optional[LoopingCall] # We start writing next reactor tick. self._start_loop() def _start_loop(self): """Start the looping call to""" if not self._looping_call: # Start a looping call which runs every tick. self._looping_call = self._clock.looping_call(self._run_once, 0) def stop(self): """Stops calling resumeProducing.""" if self._looping_call: self._looping_call.stop() self._looping_call = None def pauseProducing(self): """Implements IPushProducer""" self.stop() def resumeProducing(self): """Implements IPushProducer""" self._start_loop() def stopProducing(self): """Implements IPushProducer""" self.stop() self._producer.stopProducing() def _run_once(self): """Calls resumeProducing on producer once.""" try: self._producer.resumeProducing() except Exception: logger.exception("Failed to call resumeProducing") try: self._consumer.unregisterProducer() except Exception: pass self.stopProducing()
def __init__( self, reactor: IReactorCore, clock: Clock, store: LockStore, lock_name: str, lock_key: str, token: str, ) -> None: self._reactor = reactor self._clock = clock self._store = store self._lock_name = lock_name self._lock_key = lock_key self._token = token self._looping_call = clock.looping_call( self._renew, _RENEWAL_INTERVAL_MS, store, lock_name, lock_key, token ) self._dropped = False