Skip to content

Commit fc42c0d

Browse files
author
=
committed
Update
1 parent 182d288 commit fc42c0d

File tree

2 files changed

+13
-8
lines changed

2 files changed

+13
-8
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ The reason why this pack exists is we would like to solve several specific pain
1919
pip install ThreadPoolExecutorPlus
2020

2121
## Usage
22-
Same api as concurrent.futures.ThreadPoolExecutor , with some more control function added.
22+
Same api as concurrent.futures.ThreadPoolExecutor , with some more control function added:
2323

2424
##### set_daemon_opts(min_workers = None, max_workers = None, keep_alive_time = None)
2525

@@ -86,4 +86,9 @@ async def main():
8686
asyncio.run(main())
8787
```
8888

89+
Feature demo:
90+
```Python3
91+
# feature_demo.py
92+
8993

94+
```

ThreadPoolExecutorPlus/thread.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,14 @@ def __init__(self, max_workers=None, thread_name_prefix='',
164164
initializer: An callable used to initialize worker threads.
165165
initargs: A tuple of arguments to pass to the initializer.
166166
"""
167+
self._min_workers = 4
168+
self._keep_alive_time = 100
167169
if max_workers is None:
168170
# Use this number because ThreadPoolExecutor is often
169171
# used to overlap I/O instead of CPU work.
170172
max_workers = DEFAULT_MAXIMUM_WORKER_NUM
171-
if max_workers <= 0:
172-
raise ValueError("max_workers must be greater than 0")
173+
if max_workers <= self._min_workers:
174+
raise ValueError(f"max_workers must be greater than min_workers = {self._min_workers}")
173175

174176
if initializer is not None and not callable(initializer):
175177
raise TypeError("initializer must be a callable")
@@ -186,12 +188,10 @@ def __init__(self, max_workers=None, thread_name_prefix='',
186188
("ThreadPoolExecutor-%d" % self._counter()))
187189
self._initializer = initializer
188190
self._initargs = initargs
189-
self._min_workers = 4
190-
self._keep_alive_time = 100
191191

192192
def set_daemon_opts(self , min_workers = None, max_workers = None, keep_alive_time = None):
193-
if min_workers is not None and min_workers < 1:
194-
raise ValueError('min_workers is not allowed to set below 1')
193+
if min_workers is not None and min_workers < 2:
194+
raise ValueError('min_workers is not allowed to set below 2')
195195
if max_workers is not None and max_workers < min_workers:
196196
raise ValueError('max_workers is not allowed to set below min_workers')
197197
if min_workers is not None:
@@ -228,7 +228,7 @@ def weakref_cb(_, q=self._work_queue):
228228
# TODO(bquinlan): Should avoid creating new threads if there are more
229229
# idle threads than items in the work queue.
230230
num_threads = len(self._threads)
231-
if self._free_thread_count <= self._min_workers and num_threads < self._max_workers:
231+
if self._free_thread_count < self._min_workers and num_threads < self._max_workers:
232232
thread_name = '%s_%d' % (self._thread_name_prefix or self,
233233
num_threads)
234234
t = _CustomThread(name=thread_name,

0 commit comments

Comments
 (0)