Parallel Processing in Python

prl1

Parallel Processing is a method to execute a task simultaneously in multiple cores of the same system.

In this blog, we will briefly go through the basics of parallel processing and learn to code it in python.

Note: Just to avoid confusions between multiprocessing and multi-threading here: We have multiple threads in a single process and multi-threading is a process of scheduling multiple threads on a single core.

We will be using the “multiprocessing” module for our purposes.

So, let us find out how many processors does our CPU has:

prl2

So, my CPU has 4 processors.

Now let us talk about the two types of processes: 1. Synchronous
2. Asynchronous

Synchronous Process:

In this process, locking is involved. Meaning, if we have multiple processes running, the next process starts only is the previous process has been executed completely.

Asynchronous Process:

In these kinds of processes, the order does not matter and hence output can be in a different order than the inputs passed but it can be relatively faster than the previous one.

In the multiprocessing module, there are two classes, Pool and Process. We will prefer using Pool class as it ensures that our processes go into separate cores.

Lets us do synchronous multi-processing first:

We will take a simple example in which we have a two-dimensional array. We will traverse through the rows of the array and store the sum of each row in a list.

prl3

For synchronous processes, we will be using the ‘starmap’ method:

prl4

Now, let us solve the previous problem in a different way. We will split the input into 4 equal parts and then process them in separate processors and then merge the outputs of all those.

prl5

Now, let us see if there is a difference in the results obtained from the two ways:

prl6

We can see, there is no difference in the results obtained hence as starmap is a synchronous method. The difference in the ways is that we assigned 250 rows to each core at once later while previously, 1 row was assigned to each core. Once four rows got processed, again 4 rows were assigned and so on.

Keep Learning, Keep Sharing😊.