Multiprocessing

The goal of this lab session is to get familiar with the threading and multiprocessing packages of Python 3.

Preliminaries steps

  1. Check that you have Python 3.x installed on your machine.

  2. Install the threading and multiprocessing packages if it is not available on your system with pip3.

Threads

A single thread

  1. How are threads created in Python ?

  2. What is the purpose of start() and join() ?

  3. Create a method tired() which sleeps for 5 seconds.

  4. Create a thread which will execute tired() and start it

    1. Without join()

    2. With join()

    3. Comment the results

A race condition

We will now try to demonstrate a race condition among multiple threads.

  1. Declare a variable x=0 in your python file.

  2. Create a method global_inc() which increases the global variable x

  3. Create a similar method global_dec()

  4. Start 2 threads which will call global_inc() (resp. global_dec()) 10000 times.

    1. Print the result when both threads are finished

    2. Comment

Locks

Python provides Lock objects to manage synchronization.

  1. Look at the documentation and modify your code to avoid the race condition.

  2. Mesure the execution time with and without the lock. Discuss the results.

Processes

Another parallel package called multiprocessing is available. What is the main difference with the threading one ?

  1. Write a method using the multiprocessing api which prints the number of processors on your machine, and check you have more than 2.

  2. How can you create a process ?

  3. What are the various arguments of the Process() call

  4. What is the purpose of start() and join() ?

Processes and methods

  1. Write a method without parameters which simply prints a message.

  2. Create and start 5 processes to execute the previous method.

  3. Create another method which takes an int as a parameter and print it

  4. Create and start 20 processes to execute the previous method. As a parameter, the first process will use 1, the second 2....

    1. Execute multiple times and comment the results.

Queues

Both packages have a class Queue for exchanging information between threads/processes. The following questions should be written using both packages.

  1. Implement a producer/consumer as seen during lectures, using a Queue as the main buffer.

  2. Artificially slow the producers (sleep) so they take more time than consumers. Try to find a good balance between the number of producers and consumers according to the sleep value. Ideally, the queue should never be empty but should not grow out of control.