缺少多进程迭代
multiprocess iterations missing
multiprocess
似乎错过了某些迭代。似乎某些 hoy
没有被处理。但是,代码在 multiprocess
ing 下也比在单个循环中慢 运行s,所以它可能迭代了多次?我也从来没有见过它一直到8760。
我已经 运行 在要调试的代码中的不同位置打印语句(无法在 VS Code 中单步执行多进程)。这是缺少小时数的示例(列:hoy,处理器 ID,start/end of calc_hr
,小时间隔):
8394 13335 start 1
8394 13335 end 0
8395 13335 start 1
8395 13335 end 0
8451 13334 start 56
8451 13334 end 0
8452 13334 start 1
8452 13334 end 0
你可以看到缺少的时间似乎是进程之间的问题(即13335 13334)
CONTROLS = 'cont', 'multi', 'bi'
class ControlEnergy():
def __init__(self, name):
self.name = name
self.energy = []
def make_control_energy():
ctrls = []
for name in CONTROLS:
ctrls.append(ControlEnergy(name))
return ctrls
def shade_cases_energy(hoy):
ctrls = make_control_energy()
for case in ('a', 'b'):
for ctrl in ctrls:
pass
return ctrls
def calc_hour(hoy):
print(','.join([str(hoy), str(getpid()), 'start']))
if hoy > 6 and hoy < 15:
ctrls = shade_cases_energy(hoy)
else:
ctrls = make_control_energy()
for ctrl in ctrls:
pass
print(','.join([str(hoy), str(getpid()), 'start']))
return ctrls
N_PROCESSES = 7
period = []
if __name__ == '__main__':
if N_PROCESSES > 1:
args = [[hoy] for hoy in range(8760)]
with Pool(N_PROCESSES) as pool:
period.extend(pool.starmap(calc_hour, args))
else:
for hoy in range(8760):
period.extend(calc_hour(hoy))
我以前用过 multiprocess.Pool 几次,不知道我在这里遗漏了什么。
您的代码没有遗漏任何迭代。但是 print
不是线程安全的,所以你只是没有得到所有的打印件。如果您向打印的每个字符串添加一个换行符并将 end='', flush=True
添加到您的 print
,您将看到所有迭代。
multiprocess
似乎错过了某些迭代。似乎某些 hoy
没有被处理。但是,代码在 multiprocess
ing 下也比在单个循环中慢 运行s,所以它可能迭代了多次?我也从来没有见过它一直到8760。
我已经 运行 在要调试的代码中的不同位置打印语句(无法在 VS Code 中单步执行多进程)。这是缺少小时数的示例(列:hoy,处理器 ID,start/end of calc_hr
,小时间隔):
8394 13335 start 1
8394 13335 end 0
8395 13335 start 1
8395 13335 end 0
8451 13334 start 56
8451 13334 end 0
8452 13334 start 1
8452 13334 end 0
你可以看到缺少的时间似乎是进程之间的问题(即13335 13334)
CONTROLS = 'cont', 'multi', 'bi'
class ControlEnergy():
def __init__(self, name):
self.name = name
self.energy = []
def make_control_energy():
ctrls = []
for name in CONTROLS:
ctrls.append(ControlEnergy(name))
return ctrls
def shade_cases_energy(hoy):
ctrls = make_control_energy()
for case in ('a', 'b'):
for ctrl in ctrls:
pass
return ctrls
def calc_hour(hoy):
print(','.join([str(hoy), str(getpid()), 'start']))
if hoy > 6 and hoy < 15:
ctrls = shade_cases_energy(hoy)
else:
ctrls = make_control_energy()
for ctrl in ctrls:
pass
print(','.join([str(hoy), str(getpid()), 'start']))
return ctrls
N_PROCESSES = 7
period = []
if __name__ == '__main__':
if N_PROCESSES > 1:
args = [[hoy] for hoy in range(8760)]
with Pool(N_PROCESSES) as pool:
period.extend(pool.starmap(calc_hour, args))
else:
for hoy in range(8760):
period.extend(calc_hour(hoy))
我以前用过 multiprocess.Pool 几次,不知道我在这里遗漏了什么。
您的代码没有遗漏任何迭代。但是 print
不是线程安全的,所以你只是没有得到所有的打印件。如果您向打印的每个字符串添加一个换行符并将 end='', flush=True
添加到您的 print
,您将看到所有迭代。