我正在尝试使用多个进程进行我的第一个模拟,现在每个模拟大约需要一个小时才能完成。我使用多处理导入。在
当我在for循环中运行simulation()时,所有代码都能正常工作。当程序运行时,池中的所有工人都开始执行相同的任务,所以我得到了4个相同的副本,但是当他们完成后,他们会一次又一次地重新开始。更重要的是,当创建池时,整个程序从第1行同时运行四次,而不仅仅是运行模拟。在
下面的代码几乎完全是多处理文档中示例的复制粘贴: http://docs.python.org/library/multiprocessing.html
我正在开发Windows7,我会尽快在linux机器上运行它,但是有人能解释一下这里出了什么问题吗。在
非常感谢
警察局。windows 7(64位)+python 2.7.1
....
code where: simulation, pulse1, pulse2, sol_ref, model, algorithm and i are defined. Also
where the various imports are made. Also some print statements.
...
if __name__=='__main__':
freeze_support()
def calculate(func, args):
result = func(*args)
return '%s says that %s%s = %s' % (
multiprocessing.current_process().name,
func.__name__, args, result
)
PROCESSES = 4
print 'Creating pool with %d processes\n' % PROCESSES
pool = Pool(PROCESSES)
print 'pool = %s' % pool
print
TASKS = [(simulation, (pulse1,pulse2,sol_ref,model,algorithm,i)) for i in delta_tau]
results = [pool.apply_async(calculate, t) for t in TASKS]
for r in results:
print '\t', r.get()
print
编辑:错误日志
打印模拟中点数的行来自ifmain语句之前的代码。在这段代码中,模拟在for循环中运行一次,以检查它是否正确工作并给出预期结果。在
^{pr2}$
在
if __name__=="__main__"
块之外定义calculate()
。子进程看不到它on Windows。在相关问题 更多 >
编程相关推荐