I have a function parallel_run
which print out the diagnostic message during the run and then return a result. I want to capture the printed message and also the returned value. However, somehow I got many error messages.
import concurrent.futures
import matplotlib.pyplot as plt
import io
import contextlib
# Define a mock function to simulate parallel_run
def parallel_run(x_z_pair, Precision, Max_iterations):
x_val, z_val = map(float, x_z_pair)
# Mock diagnostic message and result
diagnostic_message = f"Running with x={x_val}, z={z_val}, Precision={Precision}, Max_iterations={Max_iterations}"
print(diagnostic_message)
result = [(x_val, z_val), [x_val * 0.1, x_val * 0.2]] # Mock result
return result
# Function to capture printed diagnostic messages
def run_parallel_with_capture(x_z_pair, Precision, Max_iterations):
try:
f = io.StringIO()
with contextlib.redirect_stdout(f):
result = parallel_run(x_z_pair, Precision, Max_iterations)
diagnostic_message = f.getvalue().strip() # Capture and strip the diagnostic message
return diagnostic_message, result
except Exception as e:
return f"Exception: {str(e)}", None
# Generate the list of x_z_pair values
x_values = [round(x * 0.25, 2) for x in range(4, 41)] # From 1 to 10 in steps of 0.25
x_z_pairs = [(str(x), str(1.0)) for x in x_values] # Assuming z is fixed at 1.0
# Fixed parameters
Precision = 20
Max_iterations = 6
# Parallel processing function
def run_parallel(x_z_pair):
return run_parallel_with_capture(x_z_pair, Precision, Max_iterations)
if __name__ == '__main__':
results = []
with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
futures = [executor.submit(run_parallel, x_z_pair) for x_z_pair in x_z_pairs]
for future in concurrent.futures.as_completed(futures):
try:
results.append(future.result())
except Exception as exc:
print(f'Generated an exception: {exc}')
# Collect diagnostic messages and results
diagnostic_messages = [res[0] for res in results if res[1] is not None]
results_data = [res[1] for res in results if res[1] is not None]
# Extract data for plotting
x_vals = [res[0][0] for res in results_data]
y_vals = [res[1][0] for res in results_data] # Using generic names instead of delta_b_up_vals
# Plot y with respect to x_val
plt.plot(x_vals, y_vals, marker='o')
plt.xlabel('x_val')
plt.ylabel('y_val') # Using generic name instead of Delta_b_up
plt.title('y_val vs x_val') # Using generic name instead of Delta_b_up
plt.grid(True)
plt.show()
# If you need to see the diagnostic messages
for message in diagnostic_messages:
print(message)
Error message:
Generated an exception: A process in the process pool was terminated abruptly while the future was running or pending.
Generated an exception: A process in the process pool was terminated abruptly while the future was running or pending.
Generated an exception: A process in the process pool was terminated abruptly while the future was running or pending.
...
Generated an exception: A process in the process pool was terminated abruptly while the future was running or pending.
Generated an exception: A process in the process pool was terminated abruptly while the future was running or pending.
I want to know if there’s a way to collected messages and the returned values. This is a process pool, not thread pool. I don’t understand why the IO still seem to be causing trouble, since they were running independently.
How to capture printed messages in parallel process?