Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bpo-35537: subprocess uses os.posix_spawn in some cases #11452

Merged
merged 5 commits into from
Jan 15, 2019
Merged

bpo-35537: subprocess uses os.posix_spawn in some cases #11452

merged 5 commits into from
Jan 15, 2019

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Jan 7, 2019

subprocess.Popen now uses os.posix_spawn() in some cases, if:

  • os.posix_spawn() is available and properly reports errors to the
    parent process: macOS or glibc 2.26 and newer (or glibc 2.24 and
    newer on Linux).
  • executable path contains a directory
  • close_fds=False
  • preexec_fn, pass_fds, cwd, stdin, stdout, stderr
    and start_new_session parameters are not set

https://bugs.python.org/issue35537

subprocess.Popen now uses os.posix_spawn() in some cases, if:

* os.posix_spawn() is available and properly reports errors to the
  parent process: macOS or glibc 2.26 and newer (or glibc 2.24 and
  newer on Linux).
* executable path contains a directory
* close_fds=False
* preexec_fn, pass_fds, cwd, stdin, stdout, stderr
  and start_new_session parameters are not set
@vstinner
Copy link
Member Author

executable path contains a directory

Expose os.posix_spawnp() https://bugs.python.org/issue35674 will allow to remove this restriction.

@vstinner
Copy link
Member Author

@pablogsal, @gpshead; @izbyshev, @serhiy-storchaka: Would you mind to review this PR?

if libc == 'glibc' and version >= (2, 26):
# glibc 2.26 added a pipe to the POSIX implementation
# of posix_spawn() to properly report errors to the parent process.
return True
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the patch that added a pipe to the POSIX implementation also switched to unconditional use of fork. Since the main motivation for using posix_spawn appears to be performance benefits of vfork, it seems that glibc's POSIX implementation shouldn't be used at all. I suggest to remove this branch.

Otherwise, LGTM.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah. I didn't require to use vfork. But ok, I modified my PR to prefer posix_spawn() implementations which can use vfork in some cases for best performances. Let's start with a minimum platform support, and extend it later.

@vstinner
Copy link
Member Author

vstinner commented Jan 14, 2019 via email

@izbyshev
Copy link
Contributor

I meant the POSIX implementation (in sysdeps/posix/spawni.c), not the Linux one (in sysdeps/unix/sysv/linux/spawni.c) which uses clone(CLONE_VM|CLONE_VFORK) and is probably used on your laptop. In my understanding, you check for the POSIX one in glibc 2.26 branch and for the Linux one in glibc 2.24 branch.

@vstinner
Copy link
Member Author

@giampaolo: Would you mind to review this change?

Copy link
Contributor

@giampaolo giampaolo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can't comment on the usage of posix_spawn per-se (I wasn't aware it existed). The only comment I can provide is that I took a look at the discussion in bpo-35537 and the rationale seems to make sense (quite consistent speedup, nice one!). IMHO assuming using posix_spawn is safe, the patch as-is LGTM and considering the gain it makes sense to take the risk.

Nitpick: perhaps it would be good to include the benchmark results in whatsnew.

@vstinner
Copy link
Member Author

IMHO assuming using posix_spawn is safe, the patch as-is LGTM and considering the gain it makes sense to take the risk.

Honestly, I'm not 100% sure that the change is safe. But I want to make it because of the nice speedup. I prefer to push the change early during Python 3.8 devcycle, so if someone spot an issue, we have time to try to fix it, or just revert the change.

Nitpick: perhaps it would be good to include the benchmark results in whatsnew.

@serhiy-storchaka asked to not write it, at least not announce a generic "60x speedup":
#11242 (comment)

Moreover, the speedup is only enabled under very specific conditions (see the long list of conditions in my What's New in Python 3.8 entry). We can run more benchmarks later to have a better idea of the "average speedup", and so document the speedup.

@vstinner vstinner merged commit 9daecf3 into python:master Jan 15, 2019
@vstinner vstinner deleted the subprocess_spawn branch January 15, 2019 23:02
@vstinner
Copy link
Member Author

Follow-up (support pipes): PR #11575.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants