Parallel Processes in Python

Steven Bogaerts, DePauw University

Initial Publication Date: August 2, 2015

Summary

This module examines some key concepts of parallelism that have particularly accessible Python implementations, making it suitable for CS1. It considers how to create processes to work in parallel, how to use locks to manage access to shared resources, and how to pass data between processes. This is done using the multiprocessing module in Python.


Share your modifications and improvements to this activity through the Community Contribution Tool »

Learning Goals

Base Knowledge

Students should be able to:

  • Summarize what a process is and how a running program can involve many processes.
  • Describe the challenge of shared resources in parallel computation.
  • Describe the purpose of interprocess communication.

Conceptual/Applied Knowledge

Students should be able to:

  • Write embarrassingly parallel programs in Python using the multiprocessing module.
  • Manage access to a shared resource via a lock.
  • Write programs involving simple interprocess communication via a shared queue.


Context for Use

This module is designed for use in the latter half of a semester-long CS1 course. It is expected that the module would be useful in a course with any number of students, though it has been tested in courses of 20 - 30 students. At a minimum, students should have introductory-level experience in printing, variables, tuples, and writing and using functions with keyword arguments. Some basic understanding of object-oriented programming is also very helpful: classes, methods, object state, constructors, and method invocation. For example, it would be quite sufficient for students to be able to write a "Student" class with major and GPA fields and supporting accessors and mutators.


Description and Teaching Materials

You can visit the module in your browser and use it in your course:

Web version: Parallel Processes in Python

or you can download the module in either PDF format or latex format.

PDF Format: ParallelProcessesinPython.pdf.
Latex Format: latex.tar.gz.
Word Format: ParallelProcessesinPython.docx.

Teaching Notes and Tips

While the main module document is written such that it can be used as an independent tutorial, instructors may wish to present this material using a combination of in-class lecture, in-class (or lab) exercises, and out-of-class homework. In this case, the document can serve as a guide for topic ordering and scope.


Regarding prerequisites, students should have at a minimum introductory-level experience in printing, variables, tuples, and writing and using functions with keyword arguments. Some basic understanding of object-oriented programming is also helpful: classes, methods, object state, constructors, and method invocation. For example, it would be quite sufficient for students to be able to write a "Student" class with major and GPA fields and supporting accessors and mutators.

The narrative of the main module document includes several specific exercises. Typically these would be done as in-class exercises interspersed with lecture, but they could also be used as homework. These exercises include:

  • Making Multiple Processes
  • Anonymous Processes
  • Digging Holes (multiple processes cooperating on a task)
  • Extended Communication Via a Queue
  • Obtaining a Result from a Single Child

The following exercises and solutions are available from the author:

  • Short-answer questions
  • Adding Numbers programming project
  • Restaurant Simulation programming project
  • Caesar Cipher programming project
  • Parallel Pipeline Computation programming project

Assessment

Applicable assessment instruments are not yet available. The module has been class-tested several times.


Comment? Start the discussion about Parallel Processes in Python