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

Source Code for Package wtf.opi.worker

  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  Worker Models 
 19  ============= 
 20   
 21  The modules in this package implement the different ways of worker 
 22  pool models. Currently there are: 
 23   
 24  threaded 
 25    There's one single process managing a threadpool. Accepted sockets 
 26    are dispatched to a single worker thread. 
 27  """ 
 28  __author__ = u"Andr\xe9 Malo" 
 29  __docformat__ = "restructuredtext en" 
 30   
 31  from wtf.config import ConfigurationError 
 32   
 33   
34 -class WorkerInterface(object):
35 """ 36 Interface for worker implementations 37 38 :IVariables: 39 - `sig_hup`: Should SIGHUP restart the worker? 40 41 :Types: 42 - `sig_hup`: ``bool`` 43 """ 44
45 - def __init__(self, config, opts, args):
46 """ 47 Initialization 48 49 :Parameters: 50 - `config`: The application config 51 - `opts`: Command line option container 52 - `args`: Fixed commandline arguments 53 54 :Types: 55 - `config`: `wtf.config.Config` 56 - `opts`: ``optparse.OptionContainer`` 57 - `args`: ``list`` 58 """
59
60 - def setup(self, sock, prerun, parent_cleanup, child_cleanup):
61 """ 62 Setup the worker pool 63 64 :Parameters: 65 - `sock`: The main listener socket 66 - `prerun`: Pre-runner (called right after finishing the setup), called 67 once 68 - `parent_cleanup`: Parent cleanup, if the implementation forks, called 69 once 70 - `child_cleanup`: Child cleanup, if the implementation forks, called 71 every time 72 73 :return: The worker pool 74 :rtype: `WorkerPoolInterface` 75 """
76 77
78 -class WorkerPoolInterface(object):
79 """ Interface for worker pools """ 80
81 - def run(self):
82 """ Run the pool """
83
84 - def shutdown(self):
85 """ Shutdown the pool """
86 87
88 -def factory(config, opts, args):
89 """ 90 Create the worker instance selected by configuration 91 92 :Parameters: 93 - `config`: configuration 94 - `opts`: Option container 95 - `args`: Fixed arguments 96 97 :Types: 98 - `config`: `config.Config` 99 - `opts`: ``optparse.OptionContainer`` 100 - `args`: ``list`` 101 102 :return: worker instance 103 :rtype: `WorkerInterface` 104 """ 105 self = factory 106 107 model = opts.workermodel or config.wtf.workermodel 108 if model not in self.models: # pylint: disable = E1101 109 raise ConfigurationError("Unknown workermodel %r" % (model,)) 110 111 return self.models[model](config, opts, args) # pylint: disable = E1101
112 factory.models = {} # pylint: disable = W0612 113
114 -def register(name, klass):
115 """ 116 Register a worker implementation 117 118 :Parameters: 119 - `name`: The name (in the config) 120 - `klass`: The implementation class 121 122 :Types: 123 - `name`: ``str`` 124 - `klass`: `WorkerInterface` 125 """ 126 factory.models[name] = klass # pylint: disable = E1101
127