-
-
Notifications
You must be signed in to change notification settings - Fork 33.8k
bpo-29595: Expose max_queue_size in ThreadPoolExecutor #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
66c0031
693689c
e76e9a7
c2079a7
6103533
edcf3f9
cde3edd
f10a6f9
2338991
9d1a24b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -81,13 +81,15 @@ def _worker(executor_reference, work_queue): | |
| _base.LOGGER.critical('Exception in worker', exc_info=True) | ||
|
|
||
| class ThreadPoolExecutor(_base.Executor): | ||
| def __init__(self, max_workers=None, thread_name_prefix=''): | ||
| def __init__(self, max_workers=None, thread_name_prefix='', max_queue_size=0): | ||
| """Initializes a new ThreadPoolExecutor instance. | ||
|
|
||
| Args: | ||
| max_workers: The maximum number of threads that can be used to | ||
| execute the given calls. | ||
| thread_name_prefix: An optional name prefix to give our threads. | ||
| max_queue_size: The maximum number of work items to buffer before | ||
| submit() blocks, defaults to 0 (infinite). | ||
| """ | ||
| if max_workers is None: | ||
| # Use this number because ThreadPoolExecutor is often | ||
|
|
@@ -96,8 +98,12 @@ def __init__(self, max_workers=None, thread_name_prefix=''): | |
| if max_workers <= 0: | ||
| raise ValueError("max_workers must be greater than 0") | ||
|
|
||
| # check that max_queue_size is a positive integer | ||
| if type(max_queue_size) is not int or max_queue_size < 0: | ||
| raise ValueError("max_queue_size must be equal or greater 0") | ||
|
||
|
|
||
| self._max_workers = max_workers | ||
| self._work_queue = queue.Queue() | ||
| self._work_queue = queue.Queue(maxsize=max_queue_size) | ||
| self._threads = set() | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
What if this changes? Is it then still safe to just pass |
||
| self._shutdown = False | ||
| self._shutdown_lock = threading.Lock() | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -178,6 +178,25 @@ def test_thread_names_default(self): | |
| self.assertRegex(t.name, r'^.*ThreadPoolExecutor.*_[0-4]$') | ||
| t.join() | ||
|
|
||
| def test_default_max_queue_size(self): | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would add a higher-level test, i.e. create an executor with a non-zero queue size, submit a number of tasks larger than the queue size, and check that they all get executed in the end.
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good idea :) |
||
| executor = futures.ThreadPoolExecutor() | ||
| self.assertEqual(executor._work_queue.maxsize, 0) | ||
|
|
||
| def test_custom_max_queue_size(self): | ||
| for i in range(0, 10): | ||
| executor = futures.ThreadPoolExecutor(max_queue_size=i) | ||
|
||
| self.assertEqual(executor._work_queue.maxsize, i) | ||
|
|
||
| def test_negative_max_queue_size(self): | ||
| for i in range(-1, -10): | ||
| with self.assertRaises(ValueError): | ||
| futures.ThreadPoolExecutor(max_queue_size=i) | ||
|
|
||
| def test_invalid_max_queue_size(self): | ||
| for bad_value in [None, "5", False, True, 3.14]: | ||
| with self.assertRaises(ValueError): | ||
| futures.ThreadPoolExecutor(max_queue_size=bad_value) | ||
|
|
||
|
|
||
| class ProcessPoolShutdownTest(ProcessPoolMixin, ExecutorShutdownTest, unittest.TestCase): | ||
| def _prime_executor(self): | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should it strictly be an
int? Classes that inherit fromintwon't pass this condition unlessisinstanceis used. Either way I'm not sure if doing any check for the type is a good idea since theQueueclass generally doesn't (and fails when youputsomething in it)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm also fine if we leave verification to the
Queue.