<jupyter_start><jupyter_text>Let's make a function for testing This functions takes a start and end point. Then raises each element within that range to the pow_elem and sums them.<jupyter_code>def sum_pow(start, end, pow_elem=1):
    print("\nStarting Function with args", start, end, pow_elem)
    sum_arr = 0
    for x in range(start, end):
        sum_arr += (x**pow_elem)

    print("\nSum of nos. from {} to {} raised to {} is {}".format(start, end, pow_elem, sum_arr))<jupyter_output><empty_output><jupyter_text>Let's call this function multiple times<jupyter_code>def normal_run():
    sum_pow(0, 10, 1)
    sum_pow(0, 1000000, 2)
    sum_pow(0, 10000, 3)
    sum_pow(0, 100000, 5)<jupyter_output><empty_output><jupyter_text>The functions start and end in the order they were called<jupyter_code>normal_run()<jupyter_output>Starting Function with args 0 10 1

Sum of nos. from 0 to 10 raised to 1 is 45

Starting Function with args 0 1000000 2

Sum of nos. from 0 to 1000000 raised to 2 is 333332833333500000

Starting Function with args 0 10000 3

Sum of nos. from 0 to 10000 raised to 3 is 2499500025000000

Starting Function with args 0 100000 5

Sum of nos. from 0 to 100000 raised to 5 is 166661666708333333332500000000<jupyter_text>Now, we will make a multiprocessing function<jupyter_code>def multi_process_run():
    # Create processes with the function we created and different set of args
    t1 = Process(target=sum_pow, args=(0, 10, 1))
    t2 = Process(target=sum_pow, args=(0, 1000000, 2))
    t3 = Process(target=sum_pow, args=(0, 10000, 3))
    t4 = Process(target=sum_pow, args=(0, 100000, 5))
    
    # Start each process
    t1.start()
    t2.start()
    t3.start()
    t4.start()
    
    # wait for processes to finish
    t1.join()
    t2.join()
    t3.join()
    t4.join()
multi_process_run()
import multiprocessing

def modified_sum_pow(start, end, pow_elem=1):
    print("\nStarting Function with args", start, end, pow_elem)
    
    print("\nThis is Process ID: ", multiprocessing.current_process())
    sum_arr = 0
    
    for x in range(start, end):
        sum_arr += (x**pow_elem)

    print("\nSum of nos. from {} to {} raised to {} is {}".format(start, end, pow_elem, sum_arr))
 
def modified_multi_process_run():
    # Create processes with the function 
    # we created and different set of args
    t1 = Process(target=modified_sum_pow, args=(0, 10000000, 1))
    t2 = Process(target=modified_sum_pow, args=(0, 1000000, 2))
    t3 = Process(target=modified_sum_pow, args=(0, 10000, 3))
    
    # Start each process
    t1.start()
    t2.start()
    t3.start()
    
    # wait for process to finish
    t1.join()
    t2.join()
    t3.join()
modified_multi_process_run()<jupyter_output>Starting Function with args 0 10000000 1

Starting Function with args 0 1000000 2

This is Process ID:  <Process(Process-9, started)>

Starting Function with args 0 10000 3

This is Process ID:  <Process(Process-8, started)>

This is Process ID:  <Process(Process-10, started)>

Sum of nos. from 0 to 10000 raised to 3 is 2499500025000000

Sum of nos. from 0 to 1000000 raised to 2 is 333332833333500000

Sum of nos. from 0 to 10000000 raised to 1 is 49999995000000<jupyter_text>Using a Pool of Process<jupyter_code>def complicated_square_function(x):
    print("Starting function for value {}".format(x))
    time.sleep(3)
    print("Ending function for value {}\n".format(x))
    return x*x
start = time.time()

for x in range(0, 5):
    complicated_square_function(x)

end = time.time()

print("\nTime taken is {0:.4f} seconds".format(end - start))
from multiprocessing import Pool

start = time.time()

pool = Pool(processes=5)

input_list = range(0, 5)

result = pool.map(complicated_square_function, input_list) 

end = time.time()
print("Time taken is {0:.4f} seconds \n".format(end - start))
start = time.time()

pool = Pool(processes=5)

input_list = range(0, 5)

# This is a non-blocking call
result = pool.map_async(complicated_square_function, input_list)

end = time.time()
print("Time taken is {0:.4f} seconds".format(end - start))<jupyter_output>Time taken is 0.1100 seconds
Starting function for value 4
Starting function for value 3
Starting function for value 0
Starting function for value 1
Starting function for value 2
Ending function for value 4

Ending function for value 3
Ending function for value 0

Ending function for value 1


Ending function for value 2
{'fraction_non_alphanumeric': 0.1001132502831257, 'fraction_numerical': 0.08221970554926387, 'mean_word_length': 4.012485811577752, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 5, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '181162', 'license': 'MIT', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1447, 'n_words': 508, 'path': 'Section_8/Multiprocessing .ipynb', 'repo_name': 'PacktPublishing/Software-Engineering-with-Python-3.x', 'size': '4415'}