indygreg / PyOxidizer

A modern Python application packaging and distribution tool
Mozilla Public License 2.0
5.32k stars 227 forks source link

Another Multiprocessing Example, on Windows 11 #706

Open BruceEckel opened 1 year ago

BruceEckel commented 1 year ago

This works fine on CPython, but breaks when run in Pyoxidizer using python_config.run_filename:

from concurrent.futures import ProcessPoolExecutor
import math
import time
import os

def cpu_intensive(n: int, multiplier: int) -> float:
    result: float = 0
    for i in range(10_000_000 * multiplier):
        result += math.sqrt(i**3 + i**2 + i * n)
    return result

if __name__ == "__main__":
    multiplier = 1  # Increase for longer computations
    logical_processors = os.cpu_count()
    print(f"{logical_processors = }")
    tasks = (logical_processors - 0) * 1  # Try different numbers
    print(f"{tasks = }")
    start = time.monotonic()

    with ProcessPoolExecutor() as executor:
        results = executor.map(cpu_intensive, range(tasks), [multiplier] * tasks)

    print(list(results))
    print(f"Elapsed time: {time.monotonic() - start:.2f}s")

Note that I had similar issues with Nuitka, but these are the only two packagers I've tried so far.