Chapter 13
What is a process? What are the core differences between a process and a thread?
A process is an instance of a specific computer program or software that is being executed by the operating system. A process contains both the program code and its current activities and interactions with other entities. More than one thread can be implemented within the same process to access and share memory or other resources, while different processes do not interact in this way.
What is multiprocessing? What are the core differences between multiprocessing and multithreading?
Multiprocessing refers to the execution of multiple concurrent processes from an operating system, in which each process is executed on a separate CPU, as opposed to a single process at any given time. Multithreading, on the other hand, is the execution of multiple threads, which can be within the same process.
What are the API options provided by the multiprocessing module?
The multiprocessing
module provides APIs to the Process
class, which contains the implementation of a process while offering methods to spawn and interact with processes using an API similar to the threading
module. The module also provides the Pool
class, which is mainly used to implement a pool of processes, each of which will carry out the tasks submitted.
What are the core differences between the Process
class and the Pool
class from the multiprocessing module?
The Pool
class implements a pool of processes, each of which will carry out tasks submitted to a Pool
object. Generally, the Pool
class is more convenient than the Process
class, especially if the results returned from your concurrent application should be ordered.
What are the options to determine the current process in a Python program?
The multiprocessing
module provides the current_process()
method, which will return the Process
object that is currently running at any point of a program. Another way to keep track of running processes in your program is to look at the individual process IDs through the os
module.
What are daemon processes? What are their purposes, in terms of waiting for processes in a multiprocessing program?
Daemon processes run in the background and do not block the main program from exiting. This specification is common when there is not an easy way for the main program to tell if it is appropriate to interrupt the process at any given time, or when exiting the main program without completing the worker does not affect the end result.
How can you terminate a process? Why is it sometimes acceptable to terminate processes?
The terminate()
method from the multiprocessing.Process
class offers a way to quickly terminate a process. If the processes in your program never interact with the shared resources, the terminate()
method is considerably useful, especially if a process appears to be unresponsive or deadlocked.
What are the ways to facilitate interprocess communication in Python?
While locks are one of the most common synchronization primitives used for communication among threads, pipes and queues are the main way to communicate between different processes. Specifically, they provide message passing options to facilitate communication between processes: pipes for connections between two processes, and queues for multiple producers and consumers.