【发布时间】:2018-09-03 08:08:52
【问题描述】:
如何以多处理方式从函数(生成器)for x1 in x: 生成多个并行外部 exe 实例(以保持每个 cpu 线程始终运行一个 exec)?如果在下面的当前伪代码中没有方法可以做到这一点,那么其他最好/简单的解决方案是什么?
顺便说一句,在执行实例退出后,我需要获取输出文件的大小并删除它。 代码目的是找到 x/y/z 参数的理想组合,
os.system 行语法不正确以提高可读性。在state_x = x1/z1 后面会有更多的代码,比如exitcode check、getfilesize 和compare,所以x1x2x3 不会总是传递给变量。
x = list(range(1, 300+1))
y = list(range(1, 300+1))
z = list(range(1, 300+1))
state_x = []
state_y = []
state_z = []
import os
for x1 in x:
for y1 in y:
for z1 in z:
os.system("external.exe -x1 -y1 -z1 outfile_x1_y1_z1.out")
state_x = x1
state_y = y1
state_z = z1
更新1
我将代码简化为更易于理解,将os.system("external.exe... 替换为print,以便从shell 输出中更清楚代码的作用。
忽略 state_* = [] 变量总是从生成器获取最后一个循环变体,它只是简化的代码和预期的结果 - 表明代码有效!
问题还是一样,如何在循环生成器的多进程中生成 exec/print。
x = list(range(1,2+1))
y = list(range(3,4+1))
z = list(range(5,6+1))
state_x = []
state_y = []
state_z = []
import os
for x1 in x:
for y1 in y:
for z1 in z:
print (x1,y1,z1)
state_x = x1
state_y = y1
state_z = z1
外壳输出:
==================== RESTART: D:/Python36-32/myscript5.py ==============
1 3 5
1 3 6
1 4 5
1 4 6
2 3 5
2 3 6
2 4 5
2 4 6
>>> state_x
2
>>> state_y
4
>>> state_z
6
>>>
更新 2:
如果从 IDLE 运行,下面的这段代码将启动外部 exe 多处理,但我没有得到变量 state_x state_y state_z 从函数传递到全局变量。代码完成后,我在 Python Shell 中输入 state_x,我得到返回空的 []。
代码:
import itertools
import multiprocessing
import os
x = list(range(1,2+1))
y = list(range(3,4+1))
z = list(range(5,6+1))
state_x = []
state_y = []
state_z = []
def do_work(x1, y1, z1):
os.system("ping.exe 127.0.0.1 -n "+str(x1)+"")
global state_x
state_x = x1
global state_y
state_y = y1
global state_z
if __name__ == "__main__":
with multiprocessing.Pool() as pool:
results = pool.starmap(do_work, itertools.product(range(1,3),range(3,5),range(5,7), repeat=1))
【问题讨论】:
-
你读过the official docs吗?
-
@Niayesh Isky 当然,还有数百个 SO 帖子和十几个视频教程,尝试在其他论坛和 irc #python 频道提问。
标签: python python-3.x multiprocessing generator external-process