...
Lab 2.pdf
?:??
X
University of Jeddah
CCAI 412: Parallel Computing
Lab #2
Multiprocessing in Python
Multiprocessing vs Multithreading
1. In multiprocessing, each process has its own memory and there is no share memory while
in multithreading, threads access same memory.
2. Creating process is heavyweight while thread is lightweight
3. Inter-process communication little complicated and we can do it by creating a shared data
or queue
Single Processor Single Thread Single Processor Multithread
Multiprocessing
Python Example 1:
# importing the multiprocessing module
import multiprocessing
def print_cube(num):
print("Cube of ", num, ":", num**3)
def print_square(num):
print("Square of ", num, ":", num**2)
# creating processes
p1 = multiprocessing.Process(target=print_square, args=(10,))
p2 = multiprocessing.Process(target=print_cube, args=(10,))
# starting process 1
p1.start()
# starting process 2
p2.start()
# wait until process 1 is finished
p1.join()
# wait until process 2 is finished
p2.join()