Simplify implementation

pull/19299/head
Lidi Zheng 6 years ago
parent 59555d5b0c
commit 518b55a433
  1. 7
      examples/python/helloworld/greeter_server.py
  2. 3
      src/python/grpcio/grpc/__init__.py
  3. 13
      src/python/grpcio/grpc/_server.py
  4. 5
      src/python/grpcio_tests/tests/unit/_server_wait_for_termination_test.py

@ -36,11 +36,8 @@ def serve():
helloworld_pb2_grpc.add_GreeterServicer_to_server(Greeter(), server)
server.add_insecure_port('[::]:50051')
server.start()
try:
while True:
time.sleep(_ONE_DAY_IN_SECONDS)
except KeyboardInterrupt:
server.stop(0)
server.wait_for_termination()
if __name__ == '__main__':

@ -1461,6 +1461,9 @@ class Server(six.with_metaclass(abc.ABCMeta)):
Args:
timeout: A floating point number specifying a timeout for the
operation in seconds.
Returns:
A bool indicates if the operation times out.
"""
raise NotImplementedError()

@ -764,7 +764,8 @@ class _ServerState(object):
self.interceptor_pipeline = interceptor_pipeline
self.thread_pool = thread_pool
self.stage = _ServerStage.STOPPED
self.shutdown_events = []
self.termination_event = threading.Event()
self.shutdown_events = [self.termination_event]
self.maximum_concurrent_rpcs = maximum_concurrent_rpcs
self.active_rpc_count = 0
@ -959,15 +960,7 @@ class _Server(grpc.Server):
_start(self._state)
def wait_for_termination(self, timeout=None):
termination_event = threading.Event()
with self._state.lock:
if self._state.stage is _ServerStage.STOPPED:
return
else:
self._state.shutdown_events.append(termination_event)
termination_event.wait(timeout=timeout)
return self._state.termination_event.wait(timeout=timeout)
def stop(self, grace):
return _stop(self._state, grace)

@ -87,6 +87,11 @@ class ServerWaitForTerminationTest(unittest.TestCase):
termination_event.wait(timeout=test_constants.SHORT_TIMEOUT)
self.assertTrue(termination_event.is_set())
def test_just_block(self):
server = grpc.server(futures.ThreadPoolExecutor())
server.start()
server.wait_for_termination(timeout=-1)
if __name__ == '__main__':
unittest.main(verbosity=2)

Loading…
Cancel
Save