summaryrefslogtreecommitdiff
path: root/lib/py
diff options
context:
space:
mode:
authorRoger Meier <roger@apache.org>2012-04-13 14:24:55 +0000
committerRoger Meier <roger@apache.org>2012-04-13 14:24:55 +0000
commitcfff8569bb03a544f8896b5ba8611ccc4dfbb639 (patch)
tree37f9034d7f5d12750abdf21bc9276f9decfd1d15 /lib/py
parent3f5a264cd9cfafa478272d5d5d41928dc69ee39b (diff)
downloadthrift-cfff8569bb03a544f8896b5ba8611ccc4dfbb639.tar.gz
THRIFT-1569 thrift: add stop() method to python TNonblockingServer
Patch: Dave Watson git-svn-id: https://svn.apache.org/repos/asf/thrift/trunk@1325784 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'lib/py')
-rw-r--r--lib/py/src/server/TNonblockingServer.py26
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/py/src/server/TNonblockingServer.py b/lib/py/src/server/TNonblockingServer.py
index cd90b4fc0..fa478d01f 100644
--- a/lib/py/src/server/TNonblockingServer.py
+++ b/lib/py/src/server/TNonblockingServer.py
@@ -237,6 +237,7 @@ class TNonblockingServer:
self.tasks = Queue.Queue()
self._read, self._write = socket.socketpair()
self.prepared = False
+ self._stop = False
def setNumThreads(self, num):
"""Set the number of worker threads that should be created."""
@@ -246,6 +247,8 @@ class TNonblockingServer:
def prepare(self):
"""Prepares server for serve requests."""
+ if self.prepared:
+ return
self.socket.listen()
for _ in xrange(self.threads):
thread = Worker(self.tasks)
@@ -266,6 +269,21 @@ class TNonblockingServer:
"""
self._write.send('1')
+ def stop(self):
+ """Stop the server.
+
+ This method causes the serve() method to return. stop() may be invoked
+ from within your handler, or from another thread.
+
+ After stop() is called, serve() will return but the server will still
+ be listening on the socket. serve() may then be called again to resume
+ processing requests. Alternatively, close() may be called after
+ serve() returns to close the server socket and shutdown all worker
+ threads.
+ """
+ self._stop = True
+ self.wake_up()
+
def _select(self):
"""Does select on open connections."""
readable = [self.socket.handle.fileno(), self._read.fileno()]
@@ -318,7 +336,11 @@ class TNonblockingServer:
self.prepared = False
def serve(self):
- """Serve forever."""
+ """Serve requests.
+
+ Serve requests forever, or until stop() is called.
+ """
+ self._stop = False
self.prepare()
- while True:
+ while not self._stop:
self.handle()