具有共享变量的 Python 多处理/线程只能读取

标签 python multithreading multiprocessing

考虑下面的代码。我想一次运行 3 个实验。实验是独立的,它们唯一共享的是它们只读取的 Model 对象。

由于将其线程化似乎没有什么难事,我如何才能最好地在 Python 中完成这项工作?我想使用一个池左右来确保一次只运行三个实验。我应该使用多处理吗?如果是,怎样最短最简洁?

#!/usr/bin/env python2.6
import time

class Model:
    name = ""
    def __init__(self,name):
        self.name = name

class Experiment:
    id = 0
    model = None
    done = False

    def __init__(self,id,model):
        self.id = id
        self.model = model

    def run(self):
        for _ in range(0,60):
            print "Hey %s from experiment %d" % (self.model.name, id)
            time.sleep(1)
        self.done = True


if __name__ == "__main__":
    experiments = []
    model = Model("statictistical model")
    for i in range(0,5):
        experiments.append(Experiment(i, model))

    #How to run 3 experiments at the same time

最佳答案

检查文档,特别是:

http://docs.python.org/library/multiprocessing.html#module-multiprocessing.pool

确实有很多示例可以帮助您上手。例如,我可以想出:

#!/usr/bin/env python2.6
import time
import multiprocessing

class Model:
    name = ""
    def __init__(self,name):
        self.name = name

def run_experiment(id, model):
    print "Experiment %d is starting" % id
    for _ in range(0,60):
        print "Hey %s from experiment %d" % (model.name, id)
        time.sleep(1)
    print "Experiment %d is done" % id
    return "Result for %d" % id


if __name__ == "__main__":
    model = Model("statictistical model")
    experiments = ((i, model) for i in range(0, 5))
    pool = multiprocessing.Pool(3)

    results = [pool.apply_async(run_experiment, experiment) for experiment in experiments]
    for result in results:
        r = result.get()
        # do something with r
        # or nothing, i suppose...

还要注意文档中关于使用 multiprocessing 模块的内容:

Functionality within this package requires that the __main__ method be importable by the children. This is covered in Programming guidelines however it is worth pointing out here. This means that some examples, such as the multiprocessing.Pool examples will not work in the interactive interpreter

关于具有共享变量的 Python 多处理/线程只能读取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3071602/

相关文章:

python - 组合列表元素

python - 多处理:如何 mp.map 将元素存储在列表中的函数?

python - os.kill 不处理生成的进程

python - Django 模板中的分页和行格式化困难

python - 赋值前引用的MySQLdb错误局部变量(与平常不同)

multithreading - 比较和交换 - 如果 2 个处理器同时执行锁定会怎样?

c# - 简单的 C# 并发/多线程

c++ - 如何开始使用多线程编程?

multithreading - 从持久性存储读取时,CPU核心是否繁忙?

python - 使用 Python 控制对 WebDav/Apache 的访问