Package wtf :: Package opi :: Package worker :: Module single
[hide private]
[frames] | no frames]

Source Code for Module wtf.opi.worker.single

  1  # -*- coding: ascii -*- 
  2  # 
  3  # Copyright 2006-2012 
  4  # Andr\xe9 Malo or his licensors, as applicable 
  5  # 
  6  # Licensed under the Apache License, Version 2.0 (the "License"); 
  7  # you may not use this file except in compliance with the License. 
  8  # You may obtain a copy of the License at 
  9  # 
 10  #     http://www.apache.org/licenses/LICENSE-2.0 
 11  # 
 12  # Unless required by applicable law or agreed to in writing, software 
 13  # distributed under the License is distributed on an "AS IS" BASIS, 
 14  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 15  # See the License for the specific language governing permissions and 
 16  # limitations under the License. 
 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   
35 -class SingleWorker(object):
36 """ 37 Implement single worker model 38 39 :See: `wtf.opi.worker.WorkerInterface` 40 """ 41 __implements__ = [_worker.WorkerInterface] 42 sig_hup = False 43
44 - def __init__(self, config, opts, args):
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
61 -class Worker(object):
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
85 - def run(self):
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
112 - def shutdown(self):
113 """ 114 Pool shutdown 115 116 :See: `wtf.opi.worker.WorkerPoolInterface` 117 """ 118 pass
119 120
121 -class Flags(object):
122 """ Flag container for single worker """ 123 __implements__ = [_impl.FlagsInterface] 124 multithread = False 125 multiprocess = False 126 run_once = False 127
128 - def __init__(self):
129 """ Initialization """ 130 pass
131
132 - def shutdown(self):
133 """ Retrieve shutdown flag """ 134 return False
135