PreLab 11

Multiple Processes
Due by the beginning of lecture on Wednesday, November 19th

This week you will experiment with running multiple processes at the same time.

Part 1 - Creating a Process

This week, we will be working with the python multiprocessing module, which allows us to run multiple processes at the same time. To create a new process, you will first create a function that does whatever you want your process to do, and then call
p = Process(target=function_name, args=(yourargs,))
to create the process, and p.start() to run the process. Note that process uses named arguments: you will always need to put "target=" before the function name, and "args=" before the arguments. Also note that the arguments are passed in as a tuple.

1. Write code that creates a process that takes in an argument "name", and prints "Hi name".

2. How many times will the code below print "Hello"?

def printAndSpawn(n):

    print("Hello")
    for i in range(n):
        p = Process(target=printAndSpawn, args=(n-1,))
        p.start()

p = Process(target=printAndSpawn, args=(3,))
p.start()

Process Pools

A process pool is a way to create a number of threads, and have them all do the same task. To create a pool of processes, where n is the number of processes you wish to create:

pool = Pool(processes=n)
To run the processes, you will use the Pool.map function. The map function takes in a function, f, and a list of arguments, with the length of the list equal to the number of processes. (Essentially, you are handing in separate arguments for each process, in the form of a list. Note that this means your function will need to take a single argument, although that argument can be a list or a tuple.) It returns a list of the return values from the processes. This will look like:
results = pool.map(f, args_array)
3. Write code that takes in a list of numbers, creates a process pool of 4 processes, and has each process mergesort one fourth of the list. You should merge the 4 sorted lists together after they are returned by the processes. Assume you have access to a working merge function, but not a mergesort function.

Synchronization and Locks

It frequently makes sense for processes to be able to access the same variables, so they can work together. However, this can be very dangerous, as we will get incorrect results if two or more processes modify the same variable at the same time. To avoid this, we will use something called a Lock, which will only allow one process at a time to access a variable.

4. Write psuedo code for an Account class that simulates a bank account, with methods to add and withdraw money from the account.

5. Assume you have multiple processes using the same account to add and withdraw money. Which sections of code will you need to protect in order to not allow multiple processes to modify the same variable at once?