Assignment14Utkarsh
Assignment14Utkarsh
Question 1
Answer :
In Python, the multiprocessing module provides a way to create and manage multiple
processes. It offers several classes and functions to create and control processes, to
communicate and share data between them, and to handle exceptions and errors that may
occur during their execution.
1. CPU-intensive tasks: Multiprocessing can speed up the execution of tasks that require a lot of CPU time,
such as image processing, machine learning, and scientific computing.
2. Parallel programming: Multiprocessing can be used to implement parallel algorithms, where multiple
processes work together to solve a problem.
3. Scalability: Multiprocessing can help to scale up the performance of a program as the size of the data or the
complexity of the problem increases.
4. Fault-tolerance: Multiprocessing can improve the reliability of a program by isolating the processes from
each other and preventing errors in one process from affecting the others.
In [1]:
import multiprocessing
def square(n):
"""
This function returns square of a given number
"""
return n**2
if __name__ == '__main__':
pool = multiprocessing.Pool(processes=5)
# Close the pool and wait for the worker processes to finish
pool.close()
pool.join()
Question 2
Resource usage Higher resource usage, slower startup times Lower resource usage, faster startup times
Communication IPC mechanisms (pipes, queues, shared memory) Shared memory, synchronization primitives
Question 3
Answer :
1. Let's say you have a Python program that performs some time-consuming task, such as downloading large
files from the internet.
2. You want to be able to run this program in parallel, so that multiple downloads can happen at the same time,
speeding up the overall process.
3. To achieve this, you can use the multiprocessing module to create a separate process for each download,
allowing the downloads to happen in parallel.
In [2]:
# Example 1
import multiprocessing
import requests
if __name__ == '__main__':
# URLs of files to download
urls = [
'https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignment
%2013%20-%2014%20February%202023/data1.txt',
'https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignment
%2013%20-%2014%20February%202023/data2.txt',
'https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignment
%2013%20-%2014%20February%202023/data3.txt'
]
Downloaded https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignme
nt%2013%20-%2014%20February%202023/data1.txt to file1.txt
Downloaded https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignme
nt%2013%20-%2014%20February%202023/data3.txt to file3.txt
Downloaded https://raw.githubusercontent.com/utkarshg1/PWSkills-Assignments/main/Assignme
nt%2013%20-%2014%20February%202023/data2.txt to file2.txt
All downloads completed
In [3]:
#Example 2 Calculating cubes
import multiprocessing
if __name__ == '__main__':
# Creating an example array list to get cubes
arr = multiprocessing.Array('i',[2,3,4,5,6,7,8,9,10,11,12,13,14,15,16])
[8, 27, 64, 125, 216, 343, 512, 729, 1000, 1331, 1728, 2197, 2744, 3375, 4096]
Question 4
Answer :
In Python, a multiprocessing pool is a class in the multiprocessing module that provides a way to distribute
tasks across multiple CPU cores. The idea is to create a pool of worker processes that can execute tasks in
parallel, thereby reducing the time it takes to complete a large number of tasks.
The advantage of using a multiprocessing pool is that it allows you to take advantage of multiple CPU cores to
perform computations in parallel. This can lead to significant speedups for CPU-bound tasks, such as numerical
computations, image processing, or machine learning.
The Pool class in Python also provides various methods for controlling the number of worker processes, waiting
for tasks to complete, and handling errors. Overall, it's a powerful tool for scaling up your Python programs to
take advantage of modern hardware.
Question 5
In [4]:
import multiprocessing
import math
def calc_gamma(x):
"""Function to be executed by worker processes"""
result = math.gamma(x)
return result
if __name__ == '__main__':
# Create a list of values for which to calculate the gamma function
values = [0.5, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
# Close the pool and wait for the worker processes to finish
pool.close()
pool.join()
Gamma(0.5) = 1.7724538509055159
Gamma(1) = 1.0
Gamma(2) = 1.0
Gamma(3) = 2.0
Gamma(4) = 6.0
Gamma(5) = 24.0
Gamma(6) = 120.0
Gamma(7) = 720.0
Gamma(8) = 5040.0
Gamma(9) = 40320.0
Gamma(10) = 362880.0
Question 6
Answer :
In [7]:
import multiprocessing
import random
def generate_random_number(num):
"""
This function generates random numbers between 1 to 100
"""
random_number = random.randint(1, 100)
print(f"Process number {num}, random number generated : {random_number}")
if __name__ == '__main__':
# Creating a processess list
processes = []
for i in range(4):
p = multiprocessing.Process(target=generate_random_number, args=(i,))
processes.append(p)
p.start()