blob: 41e132020e1799d457924569a3fcbdfb1a063093 [file] [log] [blame]
Brian Quinlan81c4d362010-09-18 22:35:02 +00001# Copyright 2009 Brian Quinlan. All Rights Reserved.
2# Licensed to PSF under a Contributor Agreement.
3
4"""Implements ProcessPoolExecutor.
5
6The follow diagram and text describe the data-flow through the system:
7
8|======================= In-process =====================|== Out-of-process ==|
9
10+----------+ +----------+ +--------+ +-----------+ +---------+
11| | => | Work Ids | => | | => | Call Q | => | |
12| | +----------+ | | +-----------+ | |
13| | | ... | | | | ... | | |
14| | | 6 | | | | 5, call() | | |
15| | | 7 | | | | ... | | |
16| Process | | ... | | Local | +-----------+ | Process |
17| Pool | +----------+ | Worker | | #1..n |
18| Executor | | Thread | | |
19| | +----------- + | | +-----------+ | |
20| | <=> | Work Items | <=> | | <= | Result Q | <= | |
21| | +------------+ | | +-----------+ | |
22| | | 6: call() | | | | ... | | |
23| | | future | | | | 4, result | | |
24| | | ... | | | | 3, except | | |
25+----------+ +------------+ +--------+ +-----------+ +---------+
26
27Executor.submit() called:
28- creates a uniquely numbered _WorkItem and adds it to the "Work Items" dict
29- adds the id of the _WorkItem to the "Work Ids" queue
30
31Local worker thread:
32- reads work ids from the "Work Ids" queue and looks up the corresponding
33 WorkItem from the "Work Items" dict: if the work item has been cancelled then
34 it is simply removed from the dict, otherwise it is repackaged as a
35 _CallItem and put in the "Call Q". New _CallItems are put in the "Call Q"
36 until "Call Q" is full. NOTE: the size of the "Call Q" is kept small because
37 calls placed in the "Call Q" can no longer be cancelled with Future.cancel().
38- reads _ResultItems from "Result Q", updates the future stored in the
39 "Work Items" dict and deletes the dict entry
40
41Process #1..n:
42- reads _CallItems from "Call Q", executes the calls, and puts the resulting
43 _ResultItems in "Request Q"
44"""
45
46__author__ = 'Brian Quinlan (brian@sweetapp.com)'
47
48import atexit
Antoine Pitroudd696492011-06-08 17:21:55 +020049import os
Brian Quinlan81c4d362010-09-18 22:35:02 +000050from concurrent.futures import _base
51import queue
52import multiprocessing
Antoine Pitrou020436b2011-07-02 21:20:25 +020053from multiprocessing.queues import SimpleQueue, SentinelReady, Full
Brian Quinlan81c4d362010-09-18 22:35:02 +000054import threading
55import weakref
56
57# Workers are created as daemon threads and processes. This is done to allow the
58# interpreter to exit when there are still idle processes in a
59# ProcessPoolExecutor's process pool (i.e. shutdown() was not called). However,
60# allowing workers to die with the interpreter has two undesirable properties:
61# - The workers would still be running during interpretor shutdown,
62# meaning that they would fail in unpredictable ways.
63# - The workers could be killed while evaluating a work item, which could
64# be bad if the callable being evaluated has external side-effects e.g.
65# writing to a file.
66#
67# To work around this problem, an exit handler is installed which tells the
68# workers to exit when their work queues are empty and then waits until the
69# threads/processes finish.
70
Antoine Pitrouc13d4542011-03-26 19:29:44 +010071_threads_queues = weakref.WeakKeyDictionary()
Brian Quinlan81c4d362010-09-18 22:35:02 +000072_shutdown = False
73
74def _python_exit():
75 global _shutdown
76 _shutdown = True
Antoine Pitrouc13d4542011-03-26 19:29:44 +010077 items = list(_threads_queues.items())
78 for t, q in items:
79 q.put(None)
80 for t, q in items:
81 t.join()
Brian Quinlan81c4d362010-09-18 22:35:02 +000082
83# Controls how many more calls than processes will be queued in the call queue.
84# A smaller number will mean that processes spend more time idle waiting for
85# work while a larger number will make Future.cancel() succeed less frequently
86# (Futures in the call queue cannot be cancelled).
87EXTRA_QUEUED_CALLS = 1
88
89class _WorkItem(object):
90 def __init__(self, future, fn, args, kwargs):
91 self.future = future
92 self.fn = fn
93 self.args = args
94 self.kwargs = kwargs
95
96class _ResultItem(object):
97 def __init__(self, work_id, exception=None, result=None):
98 self.work_id = work_id
99 self.exception = exception
100 self.result = result
101
102class _CallItem(object):
103 def __init__(self, work_id, fn, args, kwargs):
104 self.work_id = work_id
105 self.fn = fn
106 self.args = args
107 self.kwargs = kwargs
108
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200109def _process_worker(call_queue, result_queue):
Brian Quinlan81c4d362010-09-18 22:35:02 +0000110 """Evaluates calls from call_queue and places the results in result_queue.
111
Georg Brandlfb1720b2010-12-09 18:08:43 +0000112 This worker is run in a separate process.
Brian Quinlan81c4d362010-09-18 22:35:02 +0000113
114 Args:
115 call_queue: A multiprocessing.Queue of _CallItems that will be read and
116 evaluated by the worker.
117 result_queue: A multiprocessing.Queue of _ResultItems that will written
118 to by the worker.
119 shutdown: A multiprocessing.Event that will be set as a signal to the
120 worker that it should exit when call_queue is empty.
121 """
122 while True:
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200123 call_item = call_queue.get(block=True)
124 if call_item is None:
125 # Wake up queue management thread
Antoine Pitroudd696492011-06-08 17:21:55 +0200126 result_queue.put(os.getpid())
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200127 return
Brian Quinlan81c4d362010-09-18 22:35:02 +0000128 try:
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200129 r = call_item.fn(*call_item.args, **call_item.kwargs)
130 except BaseException as e:
131 result_queue.put(_ResultItem(call_item.work_id,
132 exception=e))
Brian Quinlan81c4d362010-09-18 22:35:02 +0000133 else:
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200134 result_queue.put(_ResultItem(call_item.work_id,
135 result=r))
Brian Quinlan81c4d362010-09-18 22:35:02 +0000136
137def _add_call_item_to_queue(pending_work_items,
138 work_ids,
139 call_queue):
140 """Fills call_queue with _WorkItems from pending_work_items.
141
142 This function never blocks.
143
144 Args:
145 pending_work_items: A dict mapping work ids to _WorkItems e.g.
146 {5: <_WorkItem...>, 6: <_WorkItem...>, ...}
147 work_ids: A queue.Queue of work ids e.g. Queue([5, 6, ...]). Work ids
148 are consumed and the corresponding _WorkItems from
149 pending_work_items are transformed into _CallItems and put in
150 call_queue.
151 call_queue: A multiprocessing.Queue that will be filled with _CallItems
152 derived from _WorkItems.
153 """
154 while True:
155 if call_queue.full():
156 return
157 try:
158 work_id = work_ids.get(block=False)
159 except queue.Empty:
160 return
161 else:
162 work_item = pending_work_items[work_id]
163
164 if work_item.future.set_running_or_notify_cancel():
165 call_queue.put(_CallItem(work_id,
166 work_item.fn,
167 work_item.args,
168 work_item.kwargs),
169 block=True)
170 else:
171 del pending_work_items[work_id]
172 continue
173
Antoine Pitroub87a56a2011-05-03 16:34:42 +0200174def _queue_management_worker(executor_reference,
175 processes,
176 pending_work_items,
177 work_ids_queue,
178 call_queue,
179 result_queue):
Brian Quinlan81c4d362010-09-18 22:35:02 +0000180 """Manages the communication between this process and the worker processes.
181
182 This function is run in a local thread.
183
184 Args:
185 executor_reference: A weakref.ref to the ProcessPoolExecutor that owns
186 this thread. Used to determine if the ProcessPoolExecutor has been
187 garbage collected and that this function can exit.
188 process: A list of the multiprocessing.Process instances used as
189 workers.
190 pending_work_items: A dict mapping work ids to _WorkItems e.g.
191 {5: <_WorkItem...>, 6: <_WorkItem...>, ...}
192 work_ids_queue: A queue.Queue of work ids e.g. Queue([5, 6, ...]).
193 call_queue: A multiprocessing.Queue that will be filled with _CallItems
194 derived from _WorkItems for processing by the process workers.
195 result_queue: A multiprocessing.Queue of _ResultItems generated by the
196 process workers.
Brian Quinlan81c4d362010-09-18 22:35:02 +0000197 """
Antoine Pitrou020436b2011-07-02 21:20:25 +0200198 executor = None
199
200 def shutting_down():
201 return _shutdown or executor is None or executor._shutdown_thread
Antoine Pitroudd696492011-06-08 17:21:55 +0200202
203 def shutdown_worker():
204 # This is an upper bound
205 nb_children_alive = sum(p.is_alive() for p in processes.values())
206 for i in range(0, nb_children_alive):
Antoine Pitrou1c405b32011-07-03 13:17:06 +0200207 call_queue.put_nowait(None)
Antoine Pitroudd696492011-06-08 17:21:55 +0200208 # If .join() is not called on the created processes then
Antoine Pitrou020436b2011-07-02 21:20:25 +0200209 # some multiprocessing.Queue methods may deadlock on Mac OS X.
Antoine Pitroudd696492011-06-08 17:21:55 +0200210 for p in processes.values():
211 p.join()
212
Brian Quinlan81c4d362010-09-18 22:35:02 +0000213 while True:
214 _add_call_item_to_queue(pending_work_items,
215 work_ids_queue,
216 call_queue)
217
Antoine Pitroudd696492011-06-08 17:21:55 +0200218 sentinels = [p.sentinel for p in processes.values()]
219 assert sentinels
220 try:
221 result_item = result_queue.get(sentinels=sentinels)
222 except SentinelReady as e:
223 # Mark the process pool broken so that submits fail right now.
224 executor = executor_reference()
225 if executor is not None:
226 executor._broken = True
227 executor._shutdown_thread = True
Antoine Pitrou020436b2011-07-02 21:20:25 +0200228 executor = None
Antoine Pitroudd696492011-06-08 17:21:55 +0200229 # All futures in flight must be marked failed
230 for work_id, work_item in pending_work_items.items():
231 work_item.future.set_exception(
232 BrokenProcessPool(
233 "A process in the process pool was "
234 "terminated abruptly while the future was "
235 "running or pending."
236 ))
237 pending_work_items.clear()
238 # Terminate remaining workers forcibly: the queues or their
239 # locks may be in a dirty state and block forever.
240 for p in processes.values():
241 p.terminate()
242 for p in processes.values():
243 p.join()
244 return
245 if isinstance(result_item, int):
246 # Clean shutdown of a worker using its PID
247 # (avoids marking the executor broken)
Antoine Pitrou020436b2011-07-02 21:20:25 +0200248 assert shutting_down()
Antoine Pitroudd696492011-06-08 17:21:55 +0200249 del processes[result_item]
Antoine Pitrou020436b2011-07-02 21:20:25 +0200250 if not processes:
251 shutdown_worker()
252 return
Antoine Pitroudd696492011-06-08 17:21:55 +0200253 elif result_item is not None:
254 work_item = pending_work_items.pop(result_item.work_id, None)
255 # work_item can be None if another process terminated (see above)
256 if work_item is not None:
257 if result_item.exception:
258 work_item.future.set_exception(result_item.exception)
259 else:
260 work_item.future.set_result(result_item.result)
261 # Check whether we should start shutting down.
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100262 executor = executor_reference()
263 # No more work items can be added if:
264 # - The interpreter is shutting down OR
265 # - The executor that owns this worker has been collected OR
266 # - The executor that owns this worker has been shutdown.
Antoine Pitrou020436b2011-07-02 21:20:25 +0200267 if shutting_down():
Antoine Pitrou020436b2011-07-02 21:20:25 +0200268 try:
Antoine Pitrou1c405b32011-07-03 13:17:06 +0200269 # Since no new work items can be added, it is safe to shutdown
270 # this thread if there are no pending work items.
271 if not pending_work_items:
272 shutdown_worker()
273 return
274 else:
275 # Start shutting down by telling a process it can exit.
276 call_queue.put_nowait(None)
Antoine Pitrou020436b2011-07-02 21:20:25 +0200277 except Full:
278 # This is not a problem: we will eventually be woken up (in
Antoine Pitrou1c405b32011-07-03 13:17:06 +0200279 # result_queue.get()) and be able to send a sentinel again.
Antoine Pitrou020436b2011-07-02 21:20:25 +0200280 pass
281 executor = None
Brian Quinlan81c4d362010-09-18 22:35:02 +0000282
Martin v. Löwis9f6d48b2011-01-03 00:07:01 +0000283_system_limits_checked = False
284_system_limited = None
285def _check_system_limits():
286 global _system_limits_checked, _system_limited
287 if _system_limits_checked:
288 if _system_limited:
289 raise NotImplementedError(_system_limited)
290 _system_limits_checked = True
291 try:
292 import os
293 nsems_max = os.sysconf("SC_SEM_NSEMS_MAX")
294 except (AttributeError, ValueError):
295 # sysconf not available or setting not available
296 return
297 if nsems_max == -1:
298 # indetermine limit, assume that limit is determined
299 # by available memory only
300 return
301 if nsems_max >= 256:
302 # minimum number of semaphores available
303 # according to POSIX
304 return
305 _system_limited = "system provides too few semaphores (%d available, 256 necessary)" % nsems_max
306 raise NotImplementedError(_system_limited)
307
Antoine Pitroudd696492011-06-08 17:21:55 +0200308
309class BrokenProcessPool(RuntimeError):
310 """
311 Raised when a process in a ProcessPoolExecutor terminated abruptly
312 while a future was in the running state.
313 """
314
315
Brian Quinlan81c4d362010-09-18 22:35:02 +0000316class ProcessPoolExecutor(_base.Executor):
317 def __init__(self, max_workers=None):
318 """Initializes a new ProcessPoolExecutor instance.
319
320 Args:
321 max_workers: The maximum number of processes that can be used to
322 execute the given calls. If None or not given then as many
323 worker processes will be created as the machine has processors.
324 """
Martin v. Löwis9f6d48b2011-01-03 00:07:01 +0000325 _check_system_limits()
Brian Quinlan81c4d362010-09-18 22:35:02 +0000326
327 if max_workers is None:
328 self._max_workers = multiprocessing.cpu_count()
329 else:
330 self._max_workers = max_workers
331
332 # Make the call queue slightly larger than the number of processes to
333 # prevent the worker processes from idling. But don't make it too big
334 # because futures in the call queue cannot be cancelled.
335 self._call_queue = multiprocessing.Queue(self._max_workers +
336 EXTRA_QUEUED_CALLS)
Antoine Pitroub7877f22011-04-12 17:58:11 +0200337 self._result_queue = SimpleQueue()
Brian Quinlan81c4d362010-09-18 22:35:02 +0000338 self._work_ids = queue.Queue()
339 self._queue_management_thread = None
Antoine Pitroudd696492011-06-08 17:21:55 +0200340 # Map of pids to processes
341 self._processes = {}
Brian Quinlan81c4d362010-09-18 22:35:02 +0000342
343 # Shutdown is a two-step process.
344 self._shutdown_thread = False
Brian Quinlan81c4d362010-09-18 22:35:02 +0000345 self._shutdown_lock = threading.Lock()
Antoine Pitroudd696492011-06-08 17:21:55 +0200346 self._broken = False
Brian Quinlan81c4d362010-09-18 22:35:02 +0000347 self._queue_count = 0
348 self._pending_work_items = {}
349
350 def _start_queue_management_thread(self):
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100351 # When the executor gets lost, the weakref callback will wake up
352 # the queue management thread.
353 def weakref_cb(_, q=self._result_queue):
354 q.put(None)
Brian Quinlan81c4d362010-09-18 22:35:02 +0000355 if self._queue_management_thread is None:
Antoine Pitroudd696492011-06-08 17:21:55 +0200356 # Start the processes so that their sentinels are known.
357 self._adjust_process_count()
Brian Quinlan81c4d362010-09-18 22:35:02 +0000358 self._queue_management_thread = threading.Thread(
Antoine Pitroub87a56a2011-05-03 16:34:42 +0200359 target=_queue_management_worker,
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100360 args=(weakref.ref(self, weakref_cb),
Brian Quinlan81c4d362010-09-18 22:35:02 +0000361 self._processes,
362 self._pending_work_items,
363 self._work_ids,
364 self._call_queue,
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200365 self._result_queue))
Brian Quinlan81c4d362010-09-18 22:35:02 +0000366 self._queue_management_thread.daemon = True
367 self._queue_management_thread.start()
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100368 _threads_queues[self._queue_management_thread] = self._result_queue
Brian Quinlan81c4d362010-09-18 22:35:02 +0000369
370 def _adjust_process_count(self):
371 for _ in range(len(self._processes), self._max_workers):
372 p = multiprocessing.Process(
373 target=_process_worker,
374 args=(self._call_queue,
Antoine Pitrou27be5da2011-04-12 17:48:46 +0200375 self._result_queue))
Brian Quinlan81c4d362010-09-18 22:35:02 +0000376 p.start()
Antoine Pitroudd696492011-06-08 17:21:55 +0200377 self._processes[p.pid] = p
Brian Quinlan81c4d362010-09-18 22:35:02 +0000378
379 def submit(self, fn, *args, **kwargs):
380 with self._shutdown_lock:
Antoine Pitroudd696492011-06-08 17:21:55 +0200381 if self._broken:
382 raise BrokenProcessPool('A child process terminated '
383 'abruptly, the process pool is not usable anymore')
Brian Quinlan81c4d362010-09-18 22:35:02 +0000384 if self._shutdown_thread:
385 raise RuntimeError('cannot schedule new futures after shutdown')
386
387 f = _base.Future()
388 w = _WorkItem(f, fn, args, kwargs)
389
390 self._pending_work_items[self._queue_count] = w
391 self._work_ids.put(self._queue_count)
392 self._queue_count += 1
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100393 # Wake up queue management thread
394 self._result_queue.put(None)
Brian Quinlan81c4d362010-09-18 22:35:02 +0000395
396 self._start_queue_management_thread()
Brian Quinlan81c4d362010-09-18 22:35:02 +0000397 return f
398 submit.__doc__ = _base.Executor.submit.__doc__
399
400 def shutdown(self, wait=True):
401 with self._shutdown_lock:
402 self._shutdown_thread = True
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100403 if self._queue_management_thread:
404 # Wake up queue management thread
405 self._result_queue.put(None)
406 if wait:
Brian Quinlan81c4d362010-09-18 22:35:02 +0000407 self._queue_management_thread.join()
408 # To reduce the risk of openning too many files, remove references to
409 # objects that use file descriptors.
410 self._queue_management_thread = None
411 self._call_queue = None
412 self._result_queue = None
Brian Quinlan81c4d362010-09-18 22:35:02 +0000413 self._processes = None
414 shutdown.__doc__ = _base.Executor.shutdown.__doc__
415
416atexit.register(_python_exit)