How to capture printed messages in parallel process?

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật