Class which supports an async version of applying functions to arguments.
| 171 | self.notifier.put(None) |
| 172 | |
| 173 | class Pool(object): |
| 174 | ''' |
| 175 | Class which supports an async version of applying functions to arguments. |
| 176 | ''' |
| 177 | _wrap_exception = True |
| 178 | |
| 179 | @staticmethod |
| 180 | def Process(ctx, *args, **kwds): |
| 181 | return ctx.Process(*args, **kwds) |
| 182 | |
| 183 | def __init__(self, processes=None, initializer=None, initargs=(), |
| 184 | maxtasksperchild=None, context=None): |
| 185 | # Attributes initialized early to make sure that they exist in |
| 186 | # __del__() if __init__() raises an exception |
| 187 | self._pool = [] |
| 188 | self._state = INIT |
| 189 | |
| 190 | self._ctx = context or get_context() |
| 191 | self._setup_queues() |
| 192 | self._taskqueue = queue.SimpleQueue() |
| 193 | # The _change_notifier queue exist to wake up self._handle_workers() |
| 194 | # when the cache (self._cache) is empty or when there is a change in |
| 195 | # the _state variable of the thread that runs _handle_workers. |
| 196 | self._change_notifier = self._ctx.SimpleQueue() |
| 197 | self._cache = _PoolCache(notifier=self._change_notifier) |
| 198 | self._maxtasksperchild = maxtasksperchild |
| 199 | self._initializer = initializer |
| 200 | self._initargs = initargs |
| 201 | |
| 202 | if processes is None: |
| 203 | processes = os.process_cpu_count() or 1 |
| 204 | if processes < 1: |
| 205 | raise ValueError("Number of processes must be at least 1") |
| 206 | if maxtasksperchild is not None: |
| 207 | if not isinstance(maxtasksperchild, int) or maxtasksperchild <= 0: |
| 208 | raise ValueError("maxtasksperchild must be a positive int or None") |
| 209 | |
| 210 | if initializer is not None and not callable(initializer): |
| 211 | raise TypeError('initializer must be a callable') |
| 212 | |
| 213 | self._processes = processes |
| 214 | try: |
| 215 | self._repopulate_pool() |
| 216 | except Exception: |
| 217 | for p in self._pool: |
| 218 | if p.exitcode is None: |
| 219 | p.terminate() |
| 220 | for p in self._pool: |
| 221 | p.join() |
| 222 | raise |
| 223 | |
| 224 | sentinels = self._get_sentinels() |
| 225 | |
| 226 | self._worker_handler = threading.Thread( |
| 227 | target=Pool._handle_workers, |
| 228 | args=(self._cache, self._taskqueue, self._ctx, self.Process, |
| 229 | self._processes, self._pool, self._inqueue, self._outqueue, |
| 230 | self._initializer, self._initargs, self._maxtasksperchild, |
no outgoing calls
no test coverage detected
searching dependent graphs…