简体   繁体   English

python并行执行中变量的范围

[英]Scope of variables in python parallel execution

My issue is summarized with a simple code as follows: 我的问题用一个简单的代码总结如下:

#!/usr/bin/env python
from joblib import Parallel, delayed
import numpy as np
h =  np.ones(3)
def func(i):
    global h
    h[i] = h[i]+i
    print(h[i])
print(h)
Parallel(n_jobs=3)(delayed(func)(i) for i in range(3))
print(h)

The output is: 输出为:

[ 1.  1.  1.]
1.0
2.0
3.0
[ 1.  1.  1.]

However, I would like the h values to be modified as they are modified inside the loop. 但是,我希望在循环内修改h值时对它们进行修改。 What am I doing wrong? 我究竟做错了什么?

Edit: I tried the answer backend="threading" suggested by L_S . 编辑:我尝试了L_S建议的答案backend="threading" It served the purpose. 它达到了目的。 However, the code behaves in a serial manner. 但是,代码以串行方式运行。 The loop was started with 3 n_jobs and I could see only one python running on doing top , whereas there are 3 python executables running without specifying backend="threading" . 循环从3个n_jobs开始,我只能看到一个python运行在top ,而有3个python可执行文件在未指定backend="threading"情况下运行。

By default Parallel uses the Python multiprocessing module to fork separate Python worker processes to execute tasks concurrently on separate CPUs. 默认情况下,Parallel使用Python多处理模块来分叉单独的Python工作进程,以在单独的CPU上同时执行任务。

So, You need to pass backend="threading" 因此,您需要传递backend="threading"

Parallel(n_jobs=3, backend="threading")(delayed(func)(i) for i in range(3))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM