blob: 3ae442d98703f5597b7f82b9781f169b533c155a [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 ThreadPoolExecutor."""
5
6__author__ = 'Brian Quinlan (brian@sweetapp.com)'
7
8import atexit
9from concurrent.futures import _base
10import queue
11import threading
12import weakref
Guido van Rossumcfd46612014-09-02 10:39:18 -070013import os
Brian Quinlan81c4d362010-09-18 22:35:02 +000014
15# Workers are created as daemon threads. This is done to allow the interpreter
16# to exit when there are still idle threads in a ThreadPoolExecutor's thread
17# pool (i.e. shutdown() was not called). However, allowing workers to die with
18# the interpreter has two undesirable properties:
19# - The workers would still be running during interpretor shutdown,
20# meaning that they would fail in unpredictable ways.
21# - The workers could be killed while evaluating a work item, which could
22# be bad if the callable being evaluated has external side-effects e.g.
23# writing to a file.
24#
25# To work around this problem, an exit handler is installed which tells the
26# workers to exit when their work queues are empty and then waits until the
27# threads finish.
28
Antoine Pitrouc13d4542011-03-26 19:29:44 +010029_threads_queues = weakref.WeakKeyDictionary()
Brian Quinlan81c4d362010-09-18 22:35:02 +000030_shutdown = False
31
32def _python_exit():
33 global _shutdown
34 _shutdown = True
Antoine Pitrouc13d4542011-03-26 19:29:44 +010035 items = list(_threads_queues.items())
36 for t, q in items:
37 q.put(None)
38 for t, q in items:
39 t.join()
Brian Quinlan81c4d362010-09-18 22:35:02 +000040
41atexit.register(_python_exit)
42
43class _WorkItem(object):
44 def __init__(self, future, fn, args, kwargs):
45 self.future = future
46 self.fn = fn
47 self.args = args
48 self.kwargs = kwargs
49
50 def run(self):
51 if not self.future.set_running_or_notify_cancel():
52 return
53
54 try:
55 result = self.fn(*self.args, **self.kwargs)
56 except BaseException as e:
57 self.future.set_exception(e)
58 else:
59 self.future.set_result(result)
60
61def _worker(executor_reference, work_queue):
62 try:
63 while True:
Antoine Pitrou27be5da2011-04-12 17:48:46 +020064 work_item = work_queue.get(block=True)
65 if work_item is not None:
66 work_item.run()
Andrew Svetlov6b973742012-11-03 15:36:01 +020067 # Delete references to object. See issue16284
68 del work_item
Antoine Pitrou27be5da2011-04-12 17:48:46 +020069 continue
Antoine Pitrouc13d4542011-03-26 19:29:44 +010070 executor = executor_reference()
71 # Exit if:
72 # - The interpreter is shutting down OR
73 # - The executor that owns the worker has been collected OR
74 # - The executor that owns the worker has been shutdown.
75 if _shutdown or executor is None or executor._shutdown:
76 # Notice other workers
77 work_queue.put(None)
78 return
79 del executor
Florent Xicluna04842a82011-11-11 20:05:50 +010080 except BaseException:
Brian Quinlan81c4d362010-09-18 22:35:02 +000081 _base.LOGGER.critical('Exception in worker', exc_info=True)
82
83class ThreadPoolExecutor(_base.Executor):
Guido van Rossumcfd46612014-09-02 10:39:18 -070084 def __init__(self, max_workers=None):
Brian Quinlan81c4d362010-09-18 22:35:02 +000085 """Initializes a new ThreadPoolExecutor instance.
86
87 Args:
88 max_workers: The maximum number of threads that can be used to
89 execute the given calls.
90 """
Guido van Rossumcfd46612014-09-02 10:39:18 -070091 if max_workers is None:
92 # Use this number because ThreadPoolExecutor is often
93 # used to overlap I/O instead of CPU work.
94 max_workers = (os.cpu_count() or 1) * 5
Brian Quinlan20efceb2014-05-17 13:51:10 -070095 if max_workers <= 0:
96 raise ValueError("max_workers must be greater than 0")
97
Brian Quinlan81c4d362010-09-18 22:35:02 +000098 self._max_workers = max_workers
99 self._work_queue = queue.Queue()
100 self._threads = set()
101 self._shutdown = False
102 self._shutdown_lock = threading.Lock()
103
104 def submit(self, fn, *args, **kwargs):
105 with self._shutdown_lock:
106 if self._shutdown:
107 raise RuntimeError('cannot schedule new futures after shutdown')
108
109 f = _base.Future()
110 w = _WorkItem(f, fn, args, kwargs)
111
112 self._work_queue.put(w)
113 self._adjust_thread_count()
114 return f
115 submit.__doc__ = _base.Executor.submit.__doc__
116
117 def _adjust_thread_count(self):
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100118 # When the executor gets lost, the weakref callback will wake up
119 # the worker threads.
120 def weakref_cb(_, q=self._work_queue):
121 q.put(None)
Brian Quinlan81c4d362010-09-18 22:35:02 +0000122 # TODO(bquinlan): Should avoid creating new threads if there are more
123 # idle threads than items in the work queue.
124 if len(self._threads) < self._max_workers:
125 t = threading.Thread(target=_worker,
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100126 args=(weakref.ref(self, weakref_cb),
127 self._work_queue))
Brian Quinlan81c4d362010-09-18 22:35:02 +0000128 t.daemon = True
129 t.start()
130 self._threads.add(t)
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100131 _threads_queues[t] = self._work_queue
Brian Quinlan81c4d362010-09-18 22:35:02 +0000132
133 def shutdown(self, wait=True):
134 with self._shutdown_lock:
135 self._shutdown = True
Antoine Pitrouc13d4542011-03-26 19:29:44 +0100136 self._work_queue.put(None)
Brian Quinlan81c4d362010-09-18 22:35:02 +0000137 if wait:
138 for t in self._threads:
139 t.join()
140 shutdown.__doc__ = _base.Executor.shutdown.__doc__