multiprocessing — True Parallelism for CPU-Bound Work — Concept Check
Questions on multiprocessing — True Parallelism for CPU-Bound Work. Each process gets its own interpreter and its own GIL — ProcessPoolExecutor spreads pure-Python computation across all cores, at the cost of process startup and pickling data between processes.
Questions in this quiz
8 questions on multiprocessing — True Parallelism for CPU-Bound Work — Concept Check. Work through them here, then start the quiz to answer them and see your score.
1.A developer is using ProcessPoolExecutor to parallelize a CPU-bound task on a Windows machine. They notice that the processes are not starting immediately. What could be the reason?
- AThe GIL is preventing processes from starting immediately.
- BThe system has insufficient CPU cores to handle the processes.
- CThe task is I/O-bound, not CPU-bound, causing delays.
- DThe __main__ guard is missing, causing issues with process spawning.
2.You have a CPU-bound task that needs to be parallelized using ProcessPoolExecutor. Which of the following should you avoid to prevent performance issues?
- APassing large data structures between processes.
- BUsing the __main__ guard in your script.
- CPassing file paths instead of file contents.
- DUsing module-level functions for tasks.
3.A developer tries to use a lambda function with ProcessPoolExecutor and encounters a PicklingError. Why does this happen?
- ALambdas cannot be pickled, so they cannot be used with ProcessPoolExecutor.
- BThe GIL prevents lambdas from being used in multiprocessing.
- CLambdas are inherently slower than regular functions.
- DThe lambda function is too complex to be processed.
4.When should you choose multiprocessing over threading for a task?
- AWhen the task is CPU-bound and written in pure Python.
- BWhen using libraries like NumPy that already leverage C.
- CWhen the task involves a lot of I/O operations.
- DWhen the task requires real-time processing.
5.A developer uses ProcessPoolExecutor for a task and observes that performance does not improve. What could be a likely reason?
- AThe task is I/O-bound, not CPU-bound.
- BThe GIL is still affecting the task's performance.
- CThe system has more than 4 CPU cores.
- DThe task is too small to benefit from parallelism.
6.What is a potential downside of using ProcessPoolExecutor for parallel processing?
- AThe requirement to run on Linux systems only.
- BThe risk of deadlocks due to process synchronization.
- CThe need to pickle data between processes.
- DThe inability to use multiple CPU cores.
7.A script using ProcessPoolExecutor runs slower on macOS compared to Linux. What might be the cause?
- AProcess startup is slower on macOS due to the spawn method.
- BPickling is less efficient on macOS.
- CThe GIL behaves differently on macOS.
- DmacOS has fewer CPU cores available.
8.Why is the __main__ guard necessary when using ProcessPoolExecutor on Windows and macOS?
- ATo enable the use of lambdas in multiprocessing.
- BTo prevent the module from being re-imported by child processes.
- CTo ensure that the GIL is correctly bypassed.
- DTo allow processes to communicate with each other.