-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
bpo-32146: multiprocessing freeze_support needed outside win32 #5195
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
Changes from 9 commits
7b90728
b0d30bc
8baf4e4
64456d9
748c2f1
3a0964a
7b49c00
26a6313
afdd553
d72742d
97e6a9d
f0ca86b
fce2afc
5a8e3c8
23e9c64
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ | |
| # Licensed to PSF under a Contributor Agreement. | ||
| # | ||
|
|
||
| import ast | ||
| import os | ||
| import sys | ||
| import runpy | ||
|
|
@@ -49,6 +50,7 @@ def get_executable(): | |
| # | ||
| # | ||
|
|
||
|
|
||
| def is_forking(argv): | ||
| ''' | ||
| Return whether commandline indicates we are forking | ||
|
|
@@ -59,19 +61,91 @@ def is_forking(argv): | |
| return False | ||
|
|
||
|
|
||
| def get_forking_args(argv): | ||
| ''' | ||
| If the command line indicated we are forking, return (args, kwargs) | ||
| suitable for passing to spawn_main. Otherwise return None. | ||
| ''' | ||
| if not is_forking(argv): | ||
| return None | ||
|
|
||
| args = [] | ||
| kwds = {} | ||
| for arg in argv[2:]: | ||
| name, value = arg.split('=') | ||
| if value == 'None': | ||
| kwds[name] = None | ||
| else: | ||
| kwds[name] = int(value) | ||
|
|
||
| return args, kwds | ||
|
|
||
|
|
||
| def get_semaphore_tracker_args(argv): | ||
| ''' | ||
| If the command line indicates we are running the semaphore tracker, | ||
| return (args, kwargs) suitable for passing to semaphore_tracker.main. | ||
| Otherwise return None. | ||
| ''' | ||
| if len(argv) < 2 or argv[1] != '--multiprocessing-semaphore-tracker': | ||
| return None | ||
|
|
||
| # command ends with main(fd) - extract fd | ||
| r = int(argv[-1].rsplit('(')[1].split(')')[0]) | ||
|
|
||
| args = [r] | ||
| kwds = {} | ||
| return args, kwds | ||
|
|
||
|
|
||
| def get_forkserver_args(argv): | ||
| ''' | ||
| If the command line indicates we are running the forkserver, return | ||
| (args, kwargs) suitable for passing to forkserver.main. Otherwise return | ||
| None. | ||
| ''' | ||
| if len(argv) < 2 or argv[1] != '--multiprocessing-forkserver': | ||
| return None | ||
|
|
||
| # command ends with main(listener_fd, alive_r, preload, **kwds) - extract | ||
| # the args and kwarfs | ||
| # listener_fd and alive_r are integers | ||
| # preload is a list | ||
| # kwds map strings to lists | ||
| main_args = argv[-1].split('main(')[1].rsplit(')', 1)[0].split(', ', 3) | ||
|
||
| listener_fd = int(main_args[0]) | ||
| alive_r = int(main_args[1]) | ||
| preload = ast.literal_eval(main_args[2]) | ||
|
|
||
| args = [listener_fd, alive_r, preload] | ||
| kwds = ast.literal_eval(main_args[3][2:]) | ||
| return args, kwds | ||
|
|
||
|
|
||
| def freeze_support(): | ||
| ''' | ||
| Run code for process object if this in not the main process | ||
| Run code for process object if this in not the main process. | ||
| ''' | ||
| if is_forking(sys.argv): | ||
| kwds = {} | ||
| for arg in sys.argv[2:]: | ||
| name, value = arg.split('=') | ||
| if value == 'None': | ||
| kwds[name] = None | ||
| else: | ||
| kwds[name] = int(value) | ||
| spawn_main(**kwds) | ||
| argv = sys.argv | ||
|
|
||
| forking_args = get_forking_args(argv) | ||
| if forking_args is not None: | ||
| args, kwds = forking_args | ||
| spawn_main(*args, **kwds) | ||
| sys.exit() | ||
|
|
||
| semaphore_tracker_args = get_semaphore_tracker_args(argv) | ||
| if semaphore_tracker_args is not None: | ||
| from multiprocessing.semaphore_tracker import main | ||
| args, kwds = semaphore_tracker_args | ||
| main(*args, **kwds) | ||
| sys.exit() | ||
|
|
||
| forkserver_args = get_forkserver_args(argv) | ||
| if get_forkserver_args(sys.argv): | ||
| from multiprocessing.forkserver import main | ||
| args, kwds = forkserver_args | ||
| main(*args, **kwds) | ||
| sys.exit() | ||
|
|
||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| ``multiprocessing.freeze_support`` now works on non-Windows platforms as | ||
| well. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Here we are parsing what gets produced in semaphore_tracker.SemaphoreTracker.ensure_running. It goes to semaphore_tracker.main - a single integer.