Add wait_for_termination method to grpc.Server

pull/19299/head
Lidi Zheng 6 years ago
parent b88a227135
commit 9f6243e824
  1. 14
      src/python/grpcio/grpc/__init__.py
  2. 11
      src/python/grpcio/grpc/_server.py

@ -1444,6 +1444,20 @@ class Server(six.with_metaclass(abc.ABCMeta)):
"""
raise NotImplementedError()
def wait_for_termination(self, grace=None):
"""Block current thread until the server stops.
The wait will not consume computational resources during blocking, and it
will block indefinitely. There are two ways to unblock:
1) Calling `stop` on the server in another thread;
2) The `__del__` of the server object is invoked.
Args:
grace: A duration of time in seconds or None.
"""
raise NotImplementedError()
################################# Functions ################################

@ -959,6 +959,17 @@ class _Server(grpc.Server):
def start(self):
_start(self._state)
def wait_for_termination(self, grace=None):
termination_event = threading.Event()
with self._state.lock:
if self._state.stage is _ServerStage.STOPPED:
raise ValueError('Failed to wait for a stopped server.')
else:
self._state.shutdown_events.append(termination_event)
termination_event.wait()
def stop(self, grace):
return _stop(self._state, grace)

Loading…
Cancel
Save