← 返回 tesla 的题目列表Parallel Runner with Exclusive Targets A and B
类型:qbank
Concurrency prompt for infra / GPU-adjacent teams: start 10 parallel copies of `main`; each copy must run target `A` or `B`, never more than one per target at a time, while keeping both targets maximally utilized.
Requirements
Start 10 parallel copies of main.
Each worker must execute run(target) with either target A or target B.
At most one run("A") may execute at a time.
At most one run("B") may execute at a time.
Maximize utilization of both targets: when both are free and workers are waiting, both should be active.
setup() and run(target) are provided and should not be modified.
Notes
A clean multiprocessing solution models the two targets as two resource tokens in a shared blocking queue. Each process takes one token, runs, then returns the token in a finally block.
A threading solution can use two semaphores or one queue with two target tokens. The important invariant is that the token is returned even if run raises.
Blocking queues are a better fit than polling flags because they encode wait-and-wakeup behavior and avoid a shared-state race around target assignment.
Avoid a global boolean without locking; it races immediately when 10 copies start in parallel.
This prompt is a signal to review concurrency primitives before infra, GPU, or parallelism-heavy Tesla interviews.
Preparation
Implement the solution with multiprocessing.Queue(['A', 'B']), wrapping run(target) in try/finally to return the token.
Write a stress test that logs start/end times and asserts no overlapping intervals for target A or target B, while allowing one of each to run concurrently.
Practice explaining the equivalent semaphore design and why crash handling or process termination may require a supervisor in production.