<s> import threading <EOL> import zmq <EOL> import leveldb <EOL> import json <EOL> import optparse <EOL> from time import sleep <EOL> class workerThread ( threading . Thread ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , context , db ) : <EOL> threading . Thread . __init__ ( self ) <EOL> self . context = context <EOL> self . db = db <EOL> self . running = True <EOL> self . processing = False <EOL> self . socket = self . context . socket ( zmq . XREQ ) <EOL> def run ( self ) : <EOL> self . socket . connect ( '<STR_LIT>' ) <EOL> while self . running : <EOL> try : <EOL> msg = self . socket . recv_multipart ( ) <EOL> except zmq . ZMQError : <EOL> self . running = False <EOL> continue <EOL> self . processing = True <EOL> if len ( msg ) != <NUM_LIT:3> : <EOL> value = '<STR_LIT:None>' <EOL> reply = [ msg [ <NUM_LIT:0> ] , value ] <EOL> self . socket . send_multipart ( reply ) <EOL> continue <EOL> id = msg [ <NUM_LIT:0> ] <EOL> op = msg [ <NUM_LIT:1> ] <EOL> data = json . loads ( msg [ <NUM_LIT:2> ] ) <EOL> reply = [ id ] <EOL> if op == '<STR_LIT>' : <EOL> try : <EOL> value = self . db . Get ( data ) <EOL> except : <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> elif op == '<STR_LIT>' : <EOL> try : <EOL> self . db . Put ( data [ <NUM_LIT:0> ] , data [ <NUM_LIT:1> ] ) <EOL> value = "<STR_LIT:True>" <EOL> except : <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> elif op == '<STR_LIT>' : <EOL> self . db . Delete ( data ) <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> elif op == '<STR_LIT>' : <EOL> start = data [ <NUM_LIT:0> ] <EOL> end = data [ <NUM_LIT:1> ] <EOL> if start and end : <EOL> try : <EOL> arr = [ ] <EOL> for value in self . db . RangeIter ( start , end ) : <EOL> arr . append ( { value [ <NUM_LIT:0> ] : value [ <NUM_LIT:1> ] } ) <EOL> reply . append ( json . dumps ( arr ) ) <EOL> except : <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> else : <EOL> try : <EOL> arr = [ ] <EOL> for value in self . db . RangeIter ( ) : <EOL> arr . append ( { value [ <NUM_LIT:0> ] : value [ <NUM_LIT:1> ] } ) <EOL> reply . append ( json . dumps ( arr ) ) <EOL> except : <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> else : <EOL> value = "<STR_LIT>" <EOL> reply . append ( value ) <EOL> self . socket . send_multipart ( reply ) <EOL> self . processing = False <EOL> def close ( self ) : <EOL> self . running = False <EOL> while self . processing : <EOL> sleep ( <NUM_LIT:1> ) <EOL> self . socket . close ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> optparser = optparse . OptionParser ( <EOL> prog = '<STR_LIT>' , <EOL> version = '<STR_LIT>' , <EOL> description = '<STR_LIT>' , <EOL> usage = '<STR_LIT>' + '<STR_LIT>' ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT:host>' , <EOL> default = '<STR_LIT>' ) <EOL> optparser . add_option ( '<STR_LIT>' , '<STR_LIT>' , dest = '<STR_LIT>' , <EOL> default = '<STR_LIT>' ) <EOL> options , arguments = optparser . parse_args ( ) <EOL> if not ( options . host and options . dbfile ) : <EOL> optparser . print_help ( ) <EOL> print "<STR_LIT>" % options . host <EOL> context = zmq . Context ( ) <EOL> frontend = context . socket ( zmq . XREP ) <EOL> frontend . bind ( options . host ) <EOL> backend = context . socket ( zmq . XREQ ) <EOL> backend . bind ( '<STR_LIT>' ) <EOL> poll = zmq . Poller ( ) <EOL> poll . register ( frontend , zmq . POLLIN ) <EOL> poll . register ( backend , zmq . POLLIN ) <EOL> db = leveldb . LevelDB ( options . dbfile ) <EOL> workers = [ ] <EOL> for i in xrange ( <NUM_LIT:3> ) : <EOL> worker = workerThread ( context , db ) <EOL> worker . start ( ) <EOL> workers . append ( worker ) <EOL> try : <EOL> while True : <EOL> sockets = dict ( poll . poll ( ) ) <EOL> if frontend in sockets : <EOL> if sockets [ frontend ] == zmq . POLLIN : <EOL> msg = frontend . recv_multipart ( ) <EOL> backend . send_multipart ( msg ) <EOL> if backend in sockets : <EOL> if sockets [ backend ] == zmq . POLLIN : <EOL> msg = backend . recv_multipart ( ) <EOL> frontend . send_multipart ( msg ) <EOL> except KeyboardInterrupt : <EOL> for worker in workers : <EOL> worker . close ( ) <EOL> frontend . close ( ) <EOL> backend . close ( ) <EOL> context . term ( ) </s>
