Learn

We have seen how to create a process in Python; however, we didn’t see any speed or efficiency benefits in our previous example.

To see the benefits of processes, let’s learn how to create multiple processes and test them out in our example.

We will create multiple processes using one of the following approaches:

import multiprocessing processes = [] # list of arguments to use args = [arg1, arg2, arg3] # iterate through the length of arguments for i in range(len(args)): # create process p = multiprocessing.Process(target=target_function, args=(args[i],) # add process to processes list processes.append(p) # start process p.start() # join each process for p in processes: p.join()

or

# list of arguments to use args = [arg1, arg2, arg3] # list comprehension to create processes list processes = [multiprocessing.Process(target=target_function, args=(args[i],)) for i in range(len(args))] # start each process for p in processes: p.start() # join each process AFTER starting each process for p in processes: p.join()

Notice that we also use .join() for processes. The reason we use the method is exactly the same!

Both approaches are effective. For the rest of the lesson, we will use the first approach. We may ask you to try the second approach later on.

Note: you can also use the second approach for the threading module!

Instructions

1.

In script.py, we have some code set up for you. greeting_with_sleep() is loaded in; however, this time, it does not have the timer code. Instead, a function called main_multiprocessing() contains the timer code. In this function, you will write code to create and initiate four processes.

Inside main_multiprocessing(), write a for loop that iterates through greetings. Inside this for loop:

  • Create a process called p that has greeting_with_sleep() as its target and takes one greeting from greetings at each iteration.
  • Add the process to the processes list.
  • Start the created process.

Create a second for loop after this one that iterates through each process in processes and .join() each one.

Finally, call main_multiprocessing().

Inside the terminal output, type python3 script.py and press Enter. To check your answer, click Check Work once your program finishes running.

The full answer for this code is in the hint. Note that we test that you use the first approach shown in the narrative to implement this code.

2.

You should see something similar to the following in your terminal:

Codecademy Chelsea Hisham Ashley says hello! says hello! says hello! says hello! Multiprocessing Elapsed Time: 2.0072066410066327 seconds

Change the following print statement:

print("says hello!")

to

print(string + " says hello!")

What do you think the order of the print statements will be?

Inside the terminal output, type python3 script.py and press Enter. To check your answer, click Check Work once your program finishes running.

Sign up to start coding

Mini Info Outline Icon
By signing up for Codecademy, you agree to Codecademy's Terms of Service & Privacy Policy.

Or sign up using:

Already have an account?