Skip to content

exec_command: support running .py scripts and -m modules via compile/exec#3362

Open
zdevito wants to merge 4 commits intogh/zdevito/204/basefrom
gh/zdevito/204/head
Open

exec_command: support running .py scripts and -m modules via compile/exec#3362
zdevito wants to merge 4 commits intogh/zdevito/204/basefrom
gh/zdevito/204/head

Conversation

@zdevito
Copy link
Copy Markdown
Contributor

@zdevito zdevito commented Apr 4, 2026

Stack from ghstack (oldest at bottom):

Add BashActor.run_python endpoint that runs a Python file in-process
using compile/exec rather than spawning a subprocess. Stdout/stderr
are redirected via contextlib.redirect_stdout/redirect_stderr to either
io.StringIO buffers (returned in the result dict) or files under
output_dir. sys.argv is patched for the duration. Module execution
(-m module) is supported by resolving the module to a file path via
importlib.util.find_spec.

exec_command detects when cmd[0] ends with .py or equals -m and
dispatches to run_python instead of the bash path.

Also extract _rank_env and _expand_subdir helpers on BashActor to
eliminate duplicated rank-env setup across run, run_streaming, and
run_python.

Differential Revision: D99526571

NOTE FOR REVIEWERS: This PR has internal Meta-specific changes or comments, please review them on Phabricator!

…exec

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!

[ghstack-poisoned]
zdevito added a commit that referenced this pull request Apr 4, 2026
…exec

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!

ghstack-source-id: 362554090
Pull Request resolved: #3362
@meta-cla meta-cla bot added the CLA Signed This label is managed by the Meta Open Source bot. label Apr 4, 2026
…ia compile/exec"

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!

[ghstack-poisoned]
zdevito added a commit that referenced this pull request Apr 4, 2026
…exec

Pull Request resolved: #3362

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.
ghstack-source-id: 362554680
@exported-using-ghexport

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!
…ia compile/exec"

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!

[ghstack-poisoned]
zdevito added a commit that referenced this pull request Apr 4, 2026
…exec

Pull Request resolved: #3362

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.
ghstack-source-id: 362556103
@exported-using-ghexport

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!
…ia compile/exec"

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!

[ghstack-poisoned]
zdevito added a commit that referenced this pull request Apr 4, 2026
…exec

Pull Request resolved: #3362

Add `BashActor.run_python` endpoint that runs a Python file in-process
using `compile`/`exec` rather than spawning a subprocess. Stdout/stderr
are redirected via `contextlib.redirect_stdout/redirect_stderr` to either
`io.StringIO` buffers (returned in the result dict) or files under
`output_dir`. `sys.argv` is patched for the duration. Module execution
(`-m module`) is supported by resolving the module to a file path via
`importlib.util.find_spec`.

`exec_command` detects when `cmd[0]` ends with `.py` or equals `-m` and
dispatches to `run_python` instead of the bash path.

Also extract `_rank_env` and `_expand_subdir` helpers on `BashActor` to
eliminate duplicated rank-env setup across `run`, `run_streaming`, and
`run_python`.
ghstack-source-id: 362590609
@exported-using-ghexport

Differential Revision: [D99526571](https://our.internmc.facebook.com/intern/diff/D99526571/)

**NOTE FOR REVIEWERS**: This PR has internal Meta-specific changes or comments, please review them on [Phabricator](https://our.internmc.facebook.com/intern/diff/D99526571/)!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Meta Open Source bot. fb-exported meta-exported

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant