Skip to content

Support multiprocessing in spawn mode - #140

Merged
freakboy3742 merged 2 commits into
beeware:mainfrom
abdnh:subprocess
Oct 15, 2026
Merged

freakboy3742 merged 2 commits into
beeware:mainfrom
abdnh:subprocess

Conversation

@abdnh

@abdnh abdnh commented Oct 1, 2026

Copy link
Copy Markdown
Contributor

This adds support for spawning Python processes.

See beeware/briefcase-windows-VisualStudio-template#128 for context.

Depends on beeware/Python-Apple-support#369

Closes #20

PR Checklist:

  • I will abide by the BeeWare Code of Conduct
  • I have read and have followed the CONTRIBUTING.md file
  • This PR was generated or assisted using an AI tool

Assisted-by: Claude Opus 5.5

Comment on lines +250 to +262
// Point sys.executable at bin/python3 so that the app can start Python subprocesses.
path = [python_home stringByAppendingPathComponent:@"bin/python3"];
if ([[NSFileManager defaultManager] isExecutableFileAtPath:path]) {
debug_log(@"Setting sys.executable: %@", path);
module_attr = PyUnicode_FromString([path UTF8String]);
if (module_attr == NULL
|| PySys_SetObject("executable", module_attr) < 0
|| PySys_SetObject("_base_executable", module_attr) < 0) {
crash_dialog(@"Could not set sys.executable");
exit(-16);
}
Py_DECREF(module_attr);
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Needs a guard here to avoid setting when subprocesses are not allowed:

Suggested change
// Point sys.executable at bin/python3 so that the app can start Python subprocesses.
path = [python_home stringByAppendingPathComponent:@"bin/python3"];
if ([[NSFileManager defaultManager] isExecutableFileAtPath:path]) {
debug_log(@"Setting sys.executable: %@", path);
module_attr = PyUnicode_FromString([path UTF8String]);
if (module_attr == NULL
|| PySys_SetObject("executable", module_attr) < 0
|| PySys_SetObject("_base_executable", module_attr) < 0) {
crash_dialog(@"Could not set sys.executable");
exit(-16);
}
Py_DECREF(module_attr);
}
{% if cookiecutter.allow_subprocesses -%}
// Point sys.executable at bin/python3 so that the app can start Python subprocesses.
path = [python_home stringByAppendingPathComponent:@"bin/python3"];
if ([[NSFileManager defaultManager] isExecutableFileAtPath:path]) {
debug_log(@"Setting sys.executable: %@", path);
module_attr = PyUnicode_FromString([path UTF8String]);
if (module_attr == NULL
|| PySys_SetObject("executable", module_attr) < 0
|| PySys_SetObject("_base_executable", module_attr) < 0) {
crash_dialog(@"Could not set sys.executable");
exit(-16);
}
Py_DECREF(module_attr);
}
{% endif -%}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Wait - I see what you've done here - you're checking at runtime - nice catch.

An alternate approach: Given that calling a subprocess is explosive (especially on macOS, but generally as well), it might be a good idea to set sys.executable to None (or a similar "unusable" value) if the subprocesses aren't allowed.

@freakboy3742 freakboy3742 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As discussed on beeware/briefcase#3078, I've modified this so that sys.executable="" when subprocesses aren't allowed; otherwise, this is great - thanks for the PR!

@freakboy3742
freakboy3742 merged commit e04c79f into beeware:main Oct 15, 2026
15 checks passed
@abdnh
abdnh deleted the subprocess branch October 16, 2026 12:59
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.

Stub binary doesn't support sys.executable spawn/multiprocessing

2 participants