并行化python 2.4中的循环 [英] Parallelize a loop in python 2.4

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

问题描述

我有一些看起来像这样的代码:

I have some code that looks like this:

for item in list:
    <bunch of slow python code, depending only on item>

我想通过并行化循环来加快速度.通常, multiprocessing 模块非常适合此操作(请参见这个问题),但是它是在python 2.6中添加的,我只能使用2.4.

I want to speed this up by parallelizing the loop. Normally the multiprocessing module would be perfect for this (see the answers to this question), but it was added in python 2.6 and I'm stuck using 2.4.

在python 2.4中并行化python循环的最佳方法是什么?

What's the best way to parallelize a python loop in python 2.4?

推荐答案

您可能正在寻找叉子",这将使使用特定项目变得容易.

You might be looking for "fork," which will make it easy to use the specific item.

  • http://docs.python.org/release/2.4/lib/os-process.html
  • http://en.wikipedia.org/wiki/Fork_%28operating_system%29#Example_in_Python

不过,您的for循环看起来需要有所不同-您希望在fork返回零时立即中断.

Your for loop will need to look a little different, though -- you want to break out as soon as fork returns zero.

import os

L = ["a", "b", "c"]

for item in L:
    pid = os.fork()
    if pid == 0: break
    else: print "Forked:", pid

if pid != 0: print "Main Execution, Ends"
else: print "Execution:", item

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

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