-
Notifications
You must be signed in to change notification settings - Fork 45
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
Loky get_reusable_executor initializer fails to set global variable #359
Comments
This behavior is due to the fact that To cope with this, I think there is for now no better options than moving the global variable # module.py
import os
some_value = 0
def set_value(value):
global some_value
some_value = value
print(f"PID: {os.getpid()} :: Setter has set some_value: {some_value}")
def get_value():
return some_value And the main script: # main.py
import multiprocessing
import os
from loky import get_reusable_executor
from concurrent.futures import ProcessPoolExecutor
from module import set_value, get_value
def do_something(val):
print(f"PID: {os.getpid()} :: The some_value: {get_value()}")
return get_value() * val
def main():
n_jobs = 2
with get_reusable_executor(
max_workers=n_jobs,
initializer=set_value,
initargs=(10,),
) as executor:
result_generator = executor.map(
do_something,
range(10),
)
print(list(result_generator))
main() This gives the expected results:
|
Note that this is linked to what was discussed in #206, and that if the extra module is not possible, it is possible to have similar behavior with a hackish trick: import multiprocessing
import os
from loky import get_reusable_executor
from concurrent.futures import ProcessPoolExecutor
some_value = 0
def set_value(value):
os.some_value = value
print(f"PID: {os.getpid()} :: Setter has set some_value: {value}")
def do_something(val):
some_value = os.some_value
print(f"PID: {os.getpid()} :: The some_value: {some_value}")
return some_value * val
def main():
n_jobs = 2
with get_reusable_executor(
max_workers=n_jobs,
initializer=set_value,
initargs=(10,),
) as executor:
result_generator = executor.map(
do_something,
range(10),
)
print(list(result_generator))
main() |
Also stumbled over this limitation today! Any new recommendations since last year? |
When using the
initializer
andinitargs
ofget_reusable_executor
to set a global variable, the value of the global is lost within the child processes.A simple script describes this issue:
The output is the following because the
some_value
was not changed from its 0 value in the child processes, although clearly showing the correct value in theset_value
initializer.Replacing the
get_reusable_executor
to use more vanilla parallelization withProcessPoolExecutor
works as expected producing the following output:Loky version: 3.1.0
Python version: Python 3.9.10
The text was updated successfully, but these errors were encountered: