Permalink
Browse files

attempt to shut the workers down gracefully

  • Loading branch information...
1 parent 2722f19 commit 1a38837f9ac8029eca33f8d864195b840ecf328b @technoweenie technoweenie committed Nov 23, 2011
Showing with 24 additions and 9 deletions.
  1. +24 −9 leveldb-server.py
View
@@ -8,23 +8,32 @@
import leveldb
import json
import optparse
+from time import sleep
class workerThread(threading.Thread):
"""workerThread"""
def __init__(self, context, db):
threading.Thread.__init__ (self)
self.context = context
self.db = db
+ self.running = True
+ self.processing = False
+ self.socket = self.context.socket(zmq.XREQ)
def run(self):
- socket = self.context.socket(zmq.XREQ)
- socket.connect('inproc://backend')
- while True:
- msg = socket.recv_multipart()
+ self.socket.connect('inproc://backend')
+ while self.running:
+ try:
+ msg = self.socket.recv_multipart()
+ except zmq.ZMQError:
+ self.running = False
+ continue
+
+ self.processing = True
if len(msg) != 3:
value = 'None'
reply = [msg[0], value]
- socket.send_multipart(reply)
+ self.socket.send_multipart(reply)
continue
id = msg[0]
op = msg[1]
@@ -74,10 +83,14 @@ def run(self):
else:
value = ""
reply.append(value)
- #print reply
- socket.send_multipart(reply)
-
- socket.close()
+ self.socket.send_multipart(reply)
+ self.processing = False
+
+ def close(self):
+ self.running = False
+ while self.processing:
+ sleep(1)
+ self.socket.close()
if __name__ == "__main__":
optparser = optparse.OptionParser(
@@ -127,6 +140,8 @@ def run(self):
msg = backend.recv_multipart()
frontend.send_multipart(msg)
except KeyboardInterrupt:
+ for worker in workers:
+ worker.close()
frontend.close()
backend.close()
context.term()

0 comments on commit 1a38837

Please sign in to comment.