Reputation: 43
I want to create a pool of threads for a certain task.I want to maintain a certain number of threads at any time. How can I do that ? How can I check the number of alive threads at any time and create new threads if their count is less than the specified count ?
Upvotes: 0
Views: 813
Reputation: 365717
If you're using an existing thread pool implementation—concurrent.futures.ThreadPoolExecutor
, multiprocessing.dummy.Pool
, or anything you find on PyPI or elsewhere—this will all be taken care of automatically.
If you want to build something yourself manually, the question doesn't really make sense. You will need to keep track of your threads, start them manually, and know when they finish, so you always know the number of alive threads.
If you want sample code to look at, the source to concurrent.futures
is very readable, and pretty short. As you can see from a quick look, it calls _adjust_thread_count
every time you submit a new job, and that function just checks whether len(self._threads) < self.max_workers
and creates a new thread if needed.
Upvotes: 1