Multiprocessing.

Previously, we had shown that blocks of code could be executed in parallel using the threading module (see here and here) within Python. Let’s consider the code below.

Line 5 to 10 defines the print_rand() function where one million random numbers between 1 to 100 will be generated and each stored in the ran_list using a for loop.
Line 12 records the time when this code is executed.
Line 14 to 22 starts the two threads, each calling the function print_rand(), and waits for it to complete.
Line 24 calculate the time elapsed since the code in Line 12 was executed, allowing the time taken to run this block of code to be calculated.
Line 25 then pauses the programme for five seconds before closing it.

Python code need not be run from the IDE window; double-clicking on the .py file would run the code via a new window that appears. This window would close automatically once the code has finished running (hence Line 25 to allow for the capture of this screenshot). As shown, the time taken is about 4.86 seconds, bearing in mind that this includes the short 0.01 second pause from Line 18.

As mentioned previously, threading uses “waiting time” in a block of code to execute another block of code. It is still considered as one programme and an error in one thread may have an impact on the other thread(s). Think of it as one person performing two tasks simultaneously.

Alternatively, there is a multiprocessing module within Python. As before, the print_rand() function is called twice, but now via a different process each. The advantage of this is that an error in one process is unlikely to have an impact on the other process(es). Think of it as two person, each doing the same job.

Line 3 imports the multiprocessing module.
Line 15 and 16 starts a new process each calling the print_rand() function.
The bulk of the code remains the same as shown earlier in this blog post.
As shown below, generating two lists of one million random numbers each now takes a shorter time of 2.83 seconds.

Somehow the printout for the code executed via multiprocessing does not show up on the Python IDE console.

Is there any way to make the above codes more concise and elegant? Feel free to comment.

Leave a comment