Editorial Q&A · This problem and its answer were written by the TechLab editorial team to document a common issue. It isn't a question from a community member. If you've hit a variation of it or know a better approach, add a reply below.
A common pattern for collecting results from multiprocessing workers is a shared multiprocessing.Manager().dict(). Each worker reports its status into the dictionary, and the main process reads it at the end.
The Specific Problem
Each key in the managed dictionary is a job ID, and its value is a list of status updates for that job (shared_data['job_X'] = [{'step': 1, ...}, ...]). Workers append new status entries to their job's list.
The appends are not visible to the main process or other workers unless the whole modified list is assigned back to the key. Prints inside the worker show the list growing, but the managed dictionary never changes.
Applies to all current Python 3 versions, using only the standard library.
Code Example
import multiprocessing
import time
import os
def worker_task(worker_id, shared_data):
pid = os.getpid()
print(f"Worker {worker_id} (PID: {pid}) started.")
job_key = f"job_status_{worker_id}"
# Retrieve the list for modification.
# Assume shared_data[job_key] was pre-initialized to an empty Python list.
status_list = shared_data[job_key] # This gets a local copy of the list
# Simulate some work and append status updates
for i in range(3):
status = {"step": i + 1, "message": f"Worker {worker_id} - processing step {i+1}", "timestamp": time.time()}
print(f"Worker {worker_id} (PID: {pid}) appending locally: {status}")
status_list.append(status) # This modifies the *local* list
time.sleep(0.1)
print(f"Worker {worker_id} (PID: {pid}) finished. Final local status_list size: {len(status_list)}")
# Checking the shared_data directly from the worker also shows no updates (unless re-assigned)
print(f"Worker {worker_id} (PID: {pid}) reporting final shared_data[{job_key}] size: {len(shared_data.get(job_key, []))}")
if __name__ == "__main__":
manager = multiprocessing.Manager()
shared_data = manager.dict()
num_workers = 2
processes = []
# Pre-initialize shared_data[job_key] with an empty Python list for each worker
# This ensures a list object is present to start with.
for i in range(num_workers):
shared_data[f"job_status_{i}"] = [] # Storing an empty Python list.
print("Main process starting workers...")
for i in range(num_workers):
p = multiprocessing.Process(target=worker_task, args=(i, shared_data))
processes.append(p)
p.start()
for p in processes:
p.join()
print("\nAll workers finished. Main process checking shared data:")
for key, value in shared_data.items():
print(f" Key: {key}, Value: {value}, Type: {type(value)}")
if isinstance(value, list):
print(f" List length: {len(value)}")
manager.shutdown()
print("Manager shut down.")Expected Behavior
For each job_key (e.g., job_status_0), the main process should see a list containing 3 status dictionaries, as each worker appends 3 items to its respective list. The len() of the lists in shared_data should consistently be 3 for each job.
Actual Behavior
When the main process checks shared_data after all workers are done, the lists associated with job_status_0 and job_status_1 are empty, or sometimes contain a single item if the initial assignment was handled differently, but none of the subsequent append() calls from the workers are reflected. The print statements inside the worker show status_list growing, but this growth isn't propagated to the manager.dict().
For example, the output shows:
Key: job_status_0, Value: [], Type: <class 'list'>
List length: 0
Key: job_status_1, Value: [], Type: <class 'list'>
List length: 0There are no errors or stack traces, just missing data.
The question: why don't the appends propagate, and what's the right way to share nested mutable data between processes?
