import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
ThreadPoolExecutor:
ThreadPoolExecutor = Thread + Pool + Executor.
Pool:
The Pool portion is where it starts to get interesting. This object is going to create a pool of threads, each of which can run concurrently.
Executor :
ThreadPoolExecutor
is an Executor
subclass that uses a pool of threads to execute calls asynchronously.ThreadPoolExecutor
(max_workers=None,thread_name_prefix='',initializer=None,initargs=())None
or not given, it will default to the number of processors on the machine, multiplied by 5. (Change in python 3.5) threading.Thread
names for worker threads created by the pool for easier debugging.import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
def twiceOfNumber(x):
curr_thread = threading.current_thread()
time.sleep(x)
return curr_thread.name #return threadPoolExecuter object
if __name__ == '__main__':
executer = ThreadPoolExecutor(max_workers=5)
resultList = []
for i in range(1,11):
resultList.append(executer.submit(twiceOfNumber,i))
print([future.result() for future in resultList])
['ThreadPoolExecutor-4_0', 'ThreadPoolExecutor-4_1', 'ThreadPoolExecutor-4_2', 'ThreadPoolExecutor-4_3', 'ThreadPoolExecutor-4_4', 'ThreadPoolExecutor-4_0', 'ThreadPoolExecutor-4_1', 'ThreadPoolExecutor-4_2', 'ThreadPoolExecutor-4_3', 'ThreadPoolExecutor-4_4']
Example: 2
max_worker and thread_name_prefix passed as a parameter.
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
def twiceOfNumber(x):
curr_thread = threading.current_thread()
time.sleep(x)
return curr_thread.name #return threadPoolExecuter object but name replaced with thread_name_prefix
if __name__ == '__main__':
executer = ThreadPoolExecutor(max_workers=5,thread_name_prefix='threads')
resultList = []
for i in range(1,11):
resultList.append(executer.submit(twiceOfNumber,i))
print([future.result() for future in resultList])
['threads_0', 'threads_1', 'threads_2', 'threads_3', 'threads_4', 'threads_0', 'threads_1', 'threads_2', 'threads_3', 'threads_4']
ThreadPoolExecutor
have arguments named initializer
and initargs
which can be used to execute some callable before each of Thread/Process execution.import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
def initializer(x):
print('Initilizing task enviroment : %d'%x)
def twiceOfNumber(x):
curr_thread = threading.current_thread() # check the current thread
time.sleep(x)
return curr_thread.name # check the current thread name
if __name__ == '__main__':
executer=ThreadPoolExecutor(max_workers=5, thread_name_prefix = 'Thread', initializer=initializer, initargs=(10,))
result = executer.map(twiceOfNumber, range(1,11))
print(list(result))
Initilizing task enviroment : 10
Initilizing task enviroment : 10
Initilizing task enviroment : 10
Initilizing task enviroment : 10
Initilizing task enviroment : 10
['Thread_0', 'Thread_1', 'Thread_2', 'Thread_3', 'Thread_4', 'Thread_0', 'Thread_1', 'Thread_2', 'Thread_3', 'Thread_4']
import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
def twiceOfNumber(x):
curr_thread = threading.current_thread()
time.sleep(x)
return x*2 #return twice of number
if __name__ == '__main__':
executer=ThreadPoolExecutor(max_workers=5, thread_name_prefix = 'Thread')
futures = []
for i in range(1,11):
futures.append(executer.submit(twiceOfNumber, i)) # return a future obj that is stored in futures list
print([future.result() for future in futures]) # accessing futures list getting the value using result method
[2, 4, 6, 8, 10, 12, 14, 16, 18, 20]
Two way to submit the task to the executor pool:
map(func, *iterables)
except, func is executed asynchronously and several calls to func may be made concurrently.map()
method has argument named timeout
which except time in seconds and if provided then iterator will raise concurrent.futures.TimeoutError
if after that many second task has still not completed. import concurrent.futures
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor
import threading
import time
def concat(x):
curr_thread = threading.current_thread()
return "hello"+str(x) #string concatenation
if __name__ == '__main__':
with ThreadPoolExecutor(max_workers=5, thread_name_prefix = 'Thread') as executer:
result=executer.map(concat,range(1,10))
print(list(result))
['hello1', 'hello2', 'hello3', 'hello4', 'hello5', 'hello6', 'hello7', 'hello8', 'hello9']
Comments