示例#1
0
def server(thread_pool,
           handlers=None,
           options=None,
           maximum_concurrent_rpcs=None):
    """Creates a Server with which RPCs can be serviced.

  Args:
    thread_pool: A futures.ThreadPoolExecutor to be used by the Server
      to execute RPC handlers.
    handlers: An optional list of GenericRpcHandlers used for executing RPCs.
      More handlers may be added by calling add_generic_rpc_handlers any time
      before the server is started.
    options: An optional list of key-value pairs (channel args in gRPC runtime)
    to configure the channel.
    maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server
      will service before returning RESOURCE_EXHAUSTED status, or None to
      indicate no limit.

  Returns:
    A Server object.
  """
    from grpc import _server  # pylint: disable=cyclic-import
    return _server.Server(thread_pool, () if handlers is None else handlers,
                          () if options is None else options,
                          maximum_concurrent_rpcs)
示例#2
0
def server(thread_pool,
           handlers=None,
           options=None,
           maximum_concurrent_rpcs=None):
    """Creates a Server with which RPCs can be serviced.

  Args:
    thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
      to service RPCs.
    handlers: An optional sequence of GenericRpcHandlers to be used to service
      RPCs after the returned Server is started. These handlers need not be the
      only handlers the server will use to service RPCs; other handlers may
      later be added by calling add_generic_rpc_handlers any time before the
      returned Server is started.
    options: A sequence of string-value pairs according to which to configure
      the created server.
    maximum_concurrent_rpcs: The maximum number of concurrent RPCs this server
      will service before returning status RESOURCE_EXHAUSTED, or None to
      indicate no limit.

  Returns:
    A Server with which RPCs can be serviced.
  """
    from grpc import _server  # pylint: disable=cyclic-import
    return _server.Server(thread_pool, () if handlers is None else handlers, ()
                          if options is None else options,
                          maximum_concurrent_rpcs)
示例#3
0
文件: __init__.py 项目: wuwx/grpc
def server(thread_pool, handlers=None, options=None, exit_grace=None,
           exit_shutdown_handler_grace=None):
  """Creates a Server with which RPCs can be serviced.

  Args:
    thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
      to service RPCs.
    handlers: An optional sequence of GenericRpcHandlers to be used to service
      RPCs after the returned Server is started. These handlers need not be the
      only handlers the server will use to service RPCs; other handlers may
      later be added by calling add_generic_rpc_handlers any time before the
      returned Server is started.
    options: A sequence of string-value pairs according to which to configure
      the created server.
    exit_grace:  The grace period to use when terminating
      running servers at interpreter exit.  None indicates unspecified.
    exit_shutdown_handler_grace:  The shutdown handler grace to use when
      terminating running servers at interpreter exit.  None indicates
      unspecified.

  Returns:
    A Server with which RPCs can be serviced.
  """
  from grpc import _server
  return _server.Server(thread_pool, () if handlers is None else handlers,
                        () if options is None else options, exit_grace,
                        exit_shutdown_handler_grace)
  def test_reachable_then_unreachable_channel_connectivity(self):
    server = _server.Server((), futures.ThreadPoolExecutor(max_workers=0))
    port = server.add_insecure_port('[::]:0')
    server.start()
    callback = _Callback()

    channel = _channel.Channel('localhost:{}'.format(port), None, None)
    channel.subscribe(callback.update, try_to_connect=True)
    callback.block_until_connectivities_satisfy(_ready_in_connectivities)
    # Now take down the server and confirm that channel readiness is repudiated.
    server.stop(None)
    callback.block_until_connectivities_satisfy(_last_connectivity_is_not_ready)
    channel.unsubscribe(callback.update)
    def test_reachable_then_unreachable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = _server.Server(thread_pool, (), ())
        port = server.add_insecure_port('[::]:0')
        server.start()
        callback = _Callback()

        channel = _channel.Channel('localhost:{}'.format(port), (), None)
        channel.subscribe(callback.update, try_to_connect=True)
        callback.block_until_connectivities_satisfy(_ready_in_connectivities)
        # Now take down the server and confirm that channel readiness is repudiated.
        server.stop(None)
        callback.block_until_connectivities_satisfy(
            _last_connectivity_is_not_ready)
        channel.unsubscribe(callback.update)
        self.assertFalse(thread_pool.was_used())
示例#6
0
def server(thread_pool, handlers=None):
    """Creates a Server with which RPCs can be serviced.

  Args:
    thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
      to service RPCs.
    handlers: An optional sequence of GenericRpcHandlers to be used to service
      RPCs after the returned Server is started. These handlers need not be the
      only handlers the server will use to service RPCs; other handlers may
      later be added by calling add_generic_rpc_handlers any time before the
      returned Server is started.

  Returns:
    A Server with which RPCs can be serviced.
  """
    from grpc import _server
    return _server.Server(thread_pool, () if handlers is None else handlers)
示例#7
0
def server(generic_rpc_handlers, thread_pool, options=None):
    """Creates a Server with which RPCs can be serviced.

  The GenericRpcHandlers passed to this function needn't be the only
  GenericRpcHandlers that will be used to serve RPCs; others may be added later
  by calling add_generic_rpc_handlers any time before the returned server is
  started.

  Args:
    generic_rpc_handlers: Some number of GenericRpcHandlers that will be used
      to service RPCs after the returned Server is started.
    thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
      to service RPCs.

  Returns:
    A Server with which RPCs can be serviced.
  """
    from grpc import _server
    return _server.Server(generic_rpc_handlers, thread_pool)
示例#8
0
def server(thread_pool, handlers=None, options=None):
    """Creates a Server with which RPCs can be serviced.

  Args:
    thread_pool: A futures.ThreadPoolExecutor to be used by the returned Server
      to service RPCs.
    handlers: An optional sequence of GenericRpcHandlers to be used to service
      RPCs after the returned Server is started. These handlers need not be the
      only handlers the server will use to service RPCs; other handlers may
      later be added by calling add_generic_rpc_handlers any time before the
      returned Server is started.
    options: A sequence of string-value pairs according to which to configure
      the created server.

  Returns:
    A Server with which RPCs can be serviced.
  """
    from grpc import _server  # pylint: disable=cyclic-import
    return _server.Server(thread_pool, () if handlers is None else handlers,
                          () if options is None else options)
示例#9
0
    def test_immediately_connectable_channel_connectivity(self):
        server = _server.Server(futures.ThreadPoolExecutor(max_workers=0), ())
        port = server.add_insecure_port('[::]:0')
        server.start()
        channel = grpc.insecure_channel('localhost:{}'.format(port))
        callback = _Callback()

        ready_future = grpc.channel_ready_future(channel)
        ready_future.add_done_callback(callback.accept_value)
        self.assertIsNone(ready_future.result(test_constants.SHORT_TIMEOUT))
        value_passed_to_callback = callback.block_until_called()
        self.assertIs(ready_future, value_passed_to_callback)
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
        # Cancellation after maturity has no effect.
        ready_future.cancel()
        self.assertFalse(ready_future.cancelled())
        self.assertTrue(ready_future.done())
        self.assertFalse(ready_future.running())
示例#10
0
    def test_immediately_connectable_channel_connectivity(self):
        thread_pool = _thread_pool.RecordingThreadPool(max_workers=None)
        server = _server.Server(thread_pool, ())
        port = server.add_insecure_port('[::]:0')
        server.start()
        first_callback = _Callback()
        second_callback = _Callback()

        channel = _channel.Channel('localhost:{}'.format(port), None, None)
        channel.subscribe(first_callback.update, try_to_connect=False)
        first_connectivities = first_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will never happen because try_to_connect=True
        # has not yet been passed.
        time.sleep(test_constants.SHORT_TIMEOUT)
        second_connectivities = first_callback.connectivities()
        channel.subscribe(second_callback.update, try_to_connect=True)
        third_connectivities = first_callback.block_until_connectivities_satisfy(
            lambda connectivities: 2 <= len(connectivities))
        fourth_connectivities = second_callback.block_until_connectivities_satisfy(
            bool)
        # Wait for a connection that will happen (or may already have happened).
        first_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        second_callback.block_until_connectivities_satisfy(
            _ready_in_connectivities)
        del channel

        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 first_connectivities)
        self.assertSequenceEqual((grpc.ChannelConnectivity.IDLE, ),
                                 second_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         third_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.TRANSIENT_FAILURE,
                         fourth_connectivities)
        self.assertNotIn(grpc.ChannelConnectivity.SHUTDOWN,
                         fourth_connectivities)
        self.assertFalse(thread_pool.was_used())