"No one is harder on a talented person than the person themselves" - Linda Wilkinson ; "Trust your guts and don't follow the herd" ; "Validate direction not destination" ;

April 04, 2023

Python - Concurrency - Examples

#ConcurrentExamples.iy
#https://learning.oreilly.com/library/view/python-parallel-programming/9781785289583/ch04s02.html
import concurrent.futures
import time
number_list = [1,2,3,4,5,6,7,8,9,10]
def evaluate_item(x):
#count...just to make an operation
result_item = count(x)
#print the input item and the result
print ("item " + str(x) + " result " + str(result_item))
def count(number) :
for i in range(0,10000000):
i=i+1
return i*number
if __name__ == "__main__":
##Sequential Execution
start_time = time.process_time()
for item in number_list:
evaluate_item(item)
print ("Sequential execution in " + \
str(time.process_time() - start_time), "seconds")
#The ThreadPoolExecutor executes the given task using one of its internally pooled threads.
#It manages five threads working on its pool. Each thread takes a job out from the pool and executes it.
#When the job is executed, it takes the next job to be processed from the thread pool.
##Thread pool Execution
start_time_1 = time.process_time()
with concurrent.futures.ThreadPoolExecutor(max_workers=5)\
as executor:
for item in number_list:
executor.submit(evaluate_item, item)
print ("Thread pool execution in " + \
str(time.process_time() - start_time_1), "seconds")
##Process pool Execution
#the ProcessPoolExecutor class is an executor subclass that uses a pool of processes to execute calls asynchronously.
#However, unlike ThreadPoolExecutor, the ProcessPoolExecutor uses the multiprocessing module,
#which allows us to outflank the global interpreter lock and obtain a shorter execution time.
start_time_2 = time.process_time()
with concurrent.futures.ProcessPoolExecutor(max_workers=5)\
as executor:
for item in number_list:
executor.submit(evaluate_item, item)
print ("Process pool execution in " + \
str(time.process_time() - start_time_2), "seconds")


Keep Exploring!!!

No comments: