ThreadPoolExecutor in Python3
From Python 3.2 onward a new class called ThreadPoolExecutor was introduced in Python in concurrent.futures module to efficiently manage and create threads.
ThreadPoolExecutors provide a simple abstraction around spinning up multiple threads and using these threads to perform tasks in a concurrent fashion.
Creating ThreadPoolExecutor:
concurrent.futures.ThreadPoolExecutor(max_workers=5)
Here we instantiate an instance of our ThreadPoolExecutor and pass in the maximum number of workers that we want it to have. In this case we’ve defined it as 5 which essentially means this thread pool will only have 5 concurrent threads that can process any jobs that we submit to it.
Start The thread:
In order to give the threads within our ThreadPoolExecutor something to do we can call the submit() function which takes in a function as its primary parameter like so:
executor.submit()
Example code:
def postComments(self):
for info in usersArrs:
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
executor.submit(self.manageComments,info)def manageComments(self, info):
#commenting processing code