What is the primary purpose of the subprocess.PIPE constant?
PIPE enables interaction with the process streams.
Why this answer
It is used to indicate that a new pipe to the child process should be created for stdout or stdin.
33 questions · Interprocess Communication · All types, answers revealed
What is the primary purpose of the subprocess.PIPE constant?
PIPE enables interaction with the process streams.
Why this answer
It is used to indicate that a new pipe to the child process should be created for stdout or stdin.
Which module allows you to run external programs and interact with their input/output streams?
Subprocess is the standard for managing processes.
Why this answer
The subprocess module is the recommended way to spawn new processes and connect to their pipes.
You are using a multiprocessing.Semaphore(3). What happens when the 4th process attempts to acquire the semaphore?
This is the standard behavior for a semaphore.
Why this answer
The 4th process will block until one of the previous three processes releases the semaphore.
You are writing a Python script that needs to perform heavy I/O-bound tasks concurrently. Which module should you prioritize to minimize the overhead of global interpreter lock (GIL) contention?
Threading is efficient for I/O-bound tasks because it allows concurrent waiting.
Why this answer
The threading module is ideal for I/O-bound tasks as it releases the GIL during blocking operations, whereas multiprocessing is better for CPU-bound tasks.
If you want to run a function in a background thread, which class should you instantiate?
The Thread class is for spawning threads.
Why this answer
The threading.Thread class is the standard way to create threads.
Which object would you use to share a simple integer between processes that is safe for concurrent access?
Value provides shared memory access to a variable.
Why this answer
multiprocessing.Value is designed specifically to share single values between processes with a lock.
Which TWO of the following scenarios are best suited for the subprocess module?
Executing external binaries is a primary use case.
Why this answer
Subprocess is intended for executing external binaries and interacting with their system streams.
When using a multiprocessing.Queue to share data between processes, what happens if the queue is full and you use the put() method without a timeout?
Default behavior of put() is to block if the queue is full.
Why this answer
By default, put() is a blocking operation that will wait until a slot is available if the queue is full.
You have a thread waiting on a threading.Event. Which method should another thread call to wake up the waiting thread?
Set() triggers the Event.
Why this answer
The set() method sets the internal flag to true, causing wait() to return.
When using the multiprocessing module, why is it recommended to use a Manager object instead of a standard dictionary to share data between processes?
The manager provides a proxy that handles the necessary IPC.
Why this answer
A manager process is created to host the objects, allowing them to be shared safely between different processes using proxy objects.
Which of the following is true regarding daemon threads in Python?
Daemon threads are force-stopped by the interpreter.
Why this answer
Daemon threads are terminated abruptly when the main program exits, which can lead to incomplete operations.
Which TWO of the following are key benefits of using the multiprocessing.Pool class?
Pool manages the process lifecycle.
Why this answer
Pools simplify process management and provide convenient result retrieval mechanisms.
Which THREE of the following represent types of IPC provided by the multiprocessing module?
Pipes provide a connection between two processes.
Why this answer
Multiprocessing provides Pipes, Queues, and Managers for inter-process communication.
Which THREE of the following represent ways to synchronize access to shared data in a multi-threaded application?
Locks provide exclusive access.
Why this answer
Locks, Semaphores, and Events are core tools for controlling access and state between threads.
You are using the subprocess.run() function to execute an external command. Which argument ensures that the command's stdout is captured as a string rather than printed to the console?
This captures standard output and error automatically.
Why this answer
Setting capture_output=True automatically sets stdout and stderr to subprocess.PIPE.
When using subprocess.Popen, why is it dangerous to use shell=True with user-supplied input?
Executing arbitrary shell commands is a major security flaw.
Why this answer
It introduces a command injection vulnerability where a user can execute arbitrary shell commands.
When spawning processes on Windows, what is the default start method used by the multiprocessing module?
Spawn is the default on Windows.
Why this answer
The 'spawn' method is the default on Windows.
Two threads are modifying a shared global integer. You want to ensure that only one thread modifies the integer at a time. Which synchronization primitive is most appropriate?
A Lock provides the necessary mutual exclusion for threads.
Why this answer
A Lock (or Mutex) is the standard tool to ensure mutually exclusive access to a shared resource.
Which THREE of the following are true about the 'subprocess' module's interaction with the operating system?
The stdin/stdout arguments allow this.
Why this answer
Subprocess allows control over pipes, environments, and working directories for child processes.
Which TWO of the following are potential issues when using the 'fork' start method in multiprocessing?
Threads are often broken in the child process after a fork.
Why this answer
Forking a process inherits the parent's memory, which can lead to issues with locks and threads.
Which of the following describes the difference between Process.terminate() and Process.kill()?
SIGTERM allows cleanup, SIGKILL does not.
Why this answer
On most POSIX systems, terminate() sends SIGTERM (polite), while kill() sends SIGKILL (immediate).
Which THREE of the following features are common to both the threading and multiprocessing modules?
Both classes use start().
Why this answer
Both modules provide mechanisms for joining, starting, and using locks for synchronization.
You are using Pool.apply_async(). How do you retrieve the result of the function call?
Get() retrieves the result of the asynchronous task.
Why this answer
The apply_async method returns an AsyncResult object, and you must call the get() method on it.
Why should you use a 'with' statement when using a Lock?
It ensures the lock is released reliably.
Why this answer
It automatically acquires and releases the lock, ensuring it is released even if an exception occurs.
You are managing subprocesses using subprocess.Popen. Which attribute of the Popen object should you check to verify the exit code after the process terminates?
Returncode holds the exit status of the process.
Why this answer
The returncode attribute is populated after the process finishes or wait() is called.
Which THREE of the following are valid ways to instantiate a process in the multiprocessing module?
Subclassing is a valid design pattern.
Why this answer
Process can be instantiated directly, via target function, or by subclassing.
You are using the multiprocessing.Process class and notice that child processes are not terminating cleanly. Which method should you call to ensure the main process waits for the child process to complete?
Join() is the correct method to synchronize process completion.
Why this answer
The join() method blocks the calling thread until the process whose join() method is called terminates.
What is the result of using a Pipe to communicate between two processes if both processes attempt to write to the same end simultaneously?
Pipes are not inherently thread-safe for concurrent writes.
Why this answer
Data corruption can occur as the messages may become interleaved or mangled.
What is the primary reason to use the multiprocessing module instead of the threading module for CPU-bound tasks in Python?
Each process has its own Python interpreter and its own GIL.
Why this answer
Because of the Global Interpreter Lock (GIL), multiple threads cannot execute Python bytecode simultaneously on multiple cores.
Which TWO of the following are valid ways to synchronize threads?
Semaphores limit concurrent access.
Why this answer
Locks and Semaphores are fundamental threading synchronization primitives.
Which TWO of the following are true about the multiprocessing.Queue object?
Queues are process-safe.
Why this answer
Queues are thread-safe and process-safe, and they use internal locks and pipes.
You are using a multiprocessing.Pool. Which method should you use to apply a function to a sequence of items and get the results back in order as they complete?
Imap() yields results as they become available, in order.
Why this answer
imap() returns an iterator that yields results in the order of the inputs.
In the context of the threading module, what does the daemon flag do when set to True?
This is the definition of a daemon thread.
Why this answer
Daemon threads exit automatically when the main program finishes.
Ready to test yourself?
Try a timed practice session using only Interprocess Communication questions.