Parallel Square Calculation with Multiprocessing
Use multiprocessing.Pool to calculate the squares of a list of numbers, splitting the work across 2 processes.
Approach: split the input list into two halves, submit both halves to a 2-worker Pool.map() call, then combine and print the results in original order.
Input: One line: space-separated integers.
Output: One line: every input number's square, in original order, as a Python list.
1 2 3 4 5 6
[1, 4, 9, 16, 25, 36]
- 1 <= number of values <= 1000
Hint 1
with multiprocessing.Pool(processes=2) as pool: pool.map(calculate_squares, [numbers[:half], numbers[half:]]) runs both halves in parallel.
Hint 2
pool.map() returns a list of results in the SAME order the inputs were given, so results[0] + results[1] reconstructs the full, correctly-ordered list.
Hint 3
The if __name__ == "__main__": guard is required for multiprocessing to work correctly.
The input list is split into two halves, and Pool(processes=2).map() sends each half to a separate worker process to be squared in parallel — each process has its own interpreter and GIL, so this achieves genuine parallelism, unlike threading. map() preserves input order, so concatenating the two returned halves reconstructs the full list of squares in the original order.