Exemple #1
0
def pipe_iterator_to_consumer(iterator, consumer, active, terminate):
    """Pipes values emitted from an iterator to a stream.Consumer.

  Args:
    iterator: An iterator from which values will be emitted.
    consumer: A stream.Consumer to which values will be passed.
    active: A no-argument callable that returns True if the work being done by
      this function is still valid and should not be abandoned and False if the
      work being done by this function should be abandoned.
    terminate: A boolean indicating whether or not this function should
      terminate the given consumer after passing to it all values emitted by the
      given iterator.

  Raises:
    abandonment.Abandoned: If this function quits early after seeing False
      returned by the active function passed to it.
    Exception: This function raises whatever exceptions are raised by iterating
      over the given iterator.
  """
    for element in iterator:
        if not active():
            raise abandonment.Abandoned()

        consumer.consume(element)

        if not active():
            raise abandonment.Abandoned()
    if terminate:
        consumer.terminate()
Exemple #2
0
 def stock_reply_for_stock_request(stock_request):
     control.control()
     if active():
         return stock_pb2.StockReply(symbol=stock_request.symbol,
                                     price=_price(stock_request.symbol))
     else:
         raise abandonment.Abandoned()
Exemple #3
0
 def adaptation(request, servicer_context):
     callback = _Callback()
     if not servicer_context.add_callback(callback.cancel):
         raise abandonment.Abandoned()
     unary_unary_event(request, callback.consume_and_terminate,
                       _FaceServicerContext(servicer_context))
     return callback.draw_all_values()[0]
Exemple #4
0
 def adaptation(operator, operation_context):
   rendezvous = _control.Rendezvous(operator, operation_context)
   outcome = operation_context.add_termination_callback(rendezvous.set_outcome)
   if outcome is None:
     pool.submit(_control.pool_wrap(in_pool, operation_context), rendezvous)
     return utilities.full_subscription(rendezvous)
   else:
     raise abandonment.Abandoned()
Exemple #5
0
def _get_last_trade_price(stock_request, stock_reply_callback, control, active):
  """A unary-request, unary-response test method."""
  control.control()
  if active():
    stock_reply_callback(
        stock_pb2.StockReply(
            symbol=stock_request.symbol, price=_price(stock_request.symbol)))
  else:
    raise abandonment.Abandoned()
 def adaptation(request_iterator, servicer_context):
   callback = _Callback()
   if not servicer_context.add_callback(callback.cancel):
     raise abandonment.Abandoned()
   request_consumer = stream_unary_event(
       callback.consume_and_terminate, _FaceServicerContext(servicer_context))
   _run_request_pipe_thread(
       request_iterator, request_consumer, servicer_context)
   return callback.draw_all_values()[0]
Exemple #7
0
 def draw_all_values(self):
     with self._condition:
         while True:
             if self._cancelled:
                 raise abandonment.Abandoned()
             elif self._terminated:
                 all_values = tuple(self._values)
                 self._values = None
                 return all_values
             else:
                 self._condition.wait()
Exemple #8
0
 def draw_one_value(self):
     with self._condition:
         while True:
             if self._cancelled:
                 raise abandonment.Abandoned()
             elif self._values:
                 return self._values.pop(0)
             elif self._terminated:
                 return None
             else:
                 self._condition.wait()
Exemple #9
0
def _watch_future_trades(stock_request, stock_reply_consumer, control, active):
    """A unary-request, stream-response test method."""
    base_price = _price(stock_request.symbol)
    for index in range(stock_request.num_trades_to_watch):
        control.control()
        if active():
            stock_reply_consumer.consume(
                stock_pb2.StockReply(symbol=stock_request.symbol,
                                     price=base_price + index))
        else:
            raise abandonment.Abandoned()
    stock_reply_consumer.terminate()
Exemple #10
0
 def adaptation(request, servicer_context):
     callback = _Callback()
     if not servicer_context.add_callback(callback.cancel):
         raise abandonment.Abandoned()
     unary_stream_event(request, callback,
                        _FaceServicerContext(servicer_context))
     while True:
         response = callback.draw_one_value()
         if response is None:
             return
         else:
             yield response
Exemple #11
0
 def adaptation(request_iterator, servicer_context):
     callback = _Callback()
     if not servicer_context.add_callback(callback.cancel):
         raise abandonment.Abandoned()
     request_consumer = stream_stream_event(
         callback, _FaceServicerContext(servicer_context))
     _run_request_pipe_thread(request_iterator, request_consumer,
                              servicer_context)
     while True:
         response = callback.draw_one_value()
         if response is None:
             return
         else:
             yield response
Exemple #12
0
 def adaptation(group, method, operator, operation_context):
   rendezvous = _control.Rendezvous(operator, operation_context)
   outcome = operation_context.add_termination_callback(rendezvous.set_outcome)
   if outcome is None:
     def in_pool():
       request_consumer = multi_method(
           group, method, rendezvous, _ServicerContext(rendezvous))
       for request in rendezvous:
         request_consumer.consume(request)
       request_consumer.terminate()
     pool.submit(_control.pool_wrap(in_pool, operation_context), rendezvous)
     return utilities.full_subscription(rendezvous)
   else:
     raise abandonment.Abandoned()