ziglang / zig-pypi

The Zig programming language, packaged for PyPI
https://pypi.org/project/ziglang/
MIT License
148 stars 16 forks source link

Use os.exec rather than sys.exit(subprocess.call(...)) #6

Closed adamchainz closed 1 year ago

adamchainz commented 2 years ago

Using an os.exec* function, when available (Unix, Windows) will replace the Python wrapper process, rather than keeping it around until zig has finished. This saves memory and makes processes easier to debug.

You should be able to change:

sys.exit(subprocess.call([
    os.path.join(os.path.dirname(__file__), "{entry_name}"),
    *sys.argv[1:]
]))

to something like:

path = os.path.join(os.path.dirname(__file__), "{entry_name}")
os.execvp("zig", path, *sys.argv[1:])
whitequark commented 2 years ago

They are available on Windows but do not perform usefully.

adamchainz commented 2 years ago

Oh interesting, good to know. Perhaps there could be a check to use exec on unix only?

whitequark commented 2 years ago

There could be, but I'm worried about issues that arise from using different mechanisms on Unix and Windows. (This is why it doesn't already use os.exec.)

andrewrk commented 2 years ago

For what it's worth, the zig binary itself will use execve on Unix for zig run and CreateProcessW on Windows.

whitequark commented 2 years ago

Does it wait for the process to terminate?

andrewrk commented 2 years ago

The CreateProcessW path? Yes, I believe this would be equivalent to the status quo python code.

To be clear I'm not saying you can get execve semantics on Windows, just that zig does use execve when it can and where it makes sense to.

whitequark commented 2 years ago

No objection to using os.execvp on Unix then.