1   
  2   
  3   
  4   
  5   
  6   
  7   
  8   
  9   
 10   
 11   
 12   
 13   
 14   
 15   
 16   
 17  """ 
 18  Single Worker Model 
 19  =================== 
 20   
 21  Here's the single worker handling implemented. 
 22  """ 
 23  __author__ = u"Andr\xe9 Malo" 
 24  __docformat__ = "restructuredtext en" 
 25   
 26  import sys as _sys 
 27  import traceback as _traceback 
 28   
 29  from wtf import impl as _impl 
 30  from wtf import opi as _opi 
 31  from wtf import app as _app 
 32  from wtf.opi import worker as _worker 
 33   
 34   
 36      """ 
 37      Implement single worker model 
 38   
 39      :See: `wtf.opi.worker.WorkerInterface` 
 40      """ 
 41      __implements__ = [_worker.WorkerInterface] 
 42      sig_hup = False 
 43   
 45          """ 
 46          Initialization 
 47   
 48          :See: `wtf.opi.worker.WorkerInterface` 
 49          """ 
 50          self.config, self.opts, self.args = config, opts, args 
  51   
 52 -    def setup(self, sock, prerun, parent_cleanup, child_cleanup): 
  53          """ 
 54          Initialization 
 55   
 56          :See: `wtf.opi.worker.WorkerInterface` 
 57          """ 
 58          return Worker(self, sock, prerun, None, None) 
   59   
 60   
 62      """ Worker "pool" implementation """ 
 63      __implements__ = [_worker.WorkerPoolInterface] 
 64   
 65 -    def __init__(self, model, sock, prerun, parent_cleanup, child_cleanup): 
  66          """ 
 67          Initialization 
 68   
 69          :Parameters: 
 70           - `model`: The worker model implementation 
 71           - `sock`: The main socket 
 72           - `prerun`: Prerunner (maybe ``None``) 
 73           - `parent_cleanup`: Parent cleanup function (ignored) 
 74           - `child_cleanup`: Child cleanup function (ignored) 
 75   
 76          :Types: 
 77           - `model`: `SingleWorker` 
 78           - `sock`: ``socket.socket`` 
 79           - `prerun`: ``callable`` 
 80           - `parent_cleanup`: ``callable`` 
 81           - `child_cleanup`: ``callable`` 
 82          """ 
 83          self.model, self.sock, self.prerun = model, sock, prerun 
  84   
 86          """ 
 87          Pool runner 
 88   
 89          :See: `wtf.opi.worker.WorkerPoolInterface` 
 90          """ 
 91          model, prerun, self.prerun = self.model, self.prerun, None 
 92          impl = _impl.factory(model.config, model.opts, model.args) 
 93          app = _app.factory(model.config, model.opts, model.args) 
 94          try: 
 95              accept, handle, flags = self.sock.accept, impl.handle, Flags() 
 96              if prerun is not None: 
 97                  prerun() 
 98   
 99              while True: 
100                  try: 
101                      handle(accept(), app.call, flags) 
102                  except (SystemExit, KeyboardInterrupt, _opi.OPIDone): 
103                      raise 
104                  except: 
105                      print >> _sys.stderr, \ 
106                          "Exception caught in single worker:\n" + ''.join( 
107                              _traceback.format_exception(*_sys.exc_info()) 
108                          ) 
109          finally: 
110              app.shutdown() 
 111   
113          """ 
114          Pool shutdown 
115   
116          :See: `wtf.opi.worker.WorkerPoolInterface` 
117          """ 
118          pass 
  119   
120   
135