zig-pypi icon indicating copy to clipboard operation
zig-pypi copied to clipboard

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

Open adamchainz opened this issue 3 years ago • 7 comments
trafficstars

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:])

adamchainz avatar May 23 '22 14:05 adamchainz

They are available on Windows but do not perform usefully.

whitequark avatar May 23 '22 16:05 whitequark

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

adamchainz avatar May 23 '22 19:05 adamchainz

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.)

whitequark avatar May 23 '22 19:05 whitequark

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

andrewrk avatar May 23 '22 22:05 andrewrk

Does it wait for the process to terminate?

whitequark avatar May 23 '22 22:05 whitequark

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.

andrewrk avatar May 23 '22 23:05 andrewrk

No objection to using os.execvp on Unix then.

whitequark avatar May 23 '22 23:05 whitequark