并行化python for循环 [英] Parallelize python for-loop

查看:943
本文介绍了并行化python for循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用带有2个四核处理器的Mac Pro并行化以下python循环.

I would like to parallelize the following python loop using a Mac Pro with 2 Quad-Core processors.

result_list = []
for a in a_range:
    for b in b_range:
        for c in c_range:
            result = call_fortran_program(a, b, c)
            result_list.append(result)

在搜索中,我遇到过Cython和GIL之类的词,但我仍不清楚如何进行.

In my searches I've run across terms like Cython and GIL but it's still not clear to me on how to proceed.

推荐答案

from itertools import product
from multiprocessing import Pool

with Pool(processes=4) as pool:  # assuming Python 3
    pool.starmap(print, product(range(2), range(3), range(4)))

这篇关于并行化python for循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆