gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 11:03:48 25 4
gpt4 key购买 nike

考虑下面的代码。我想一次运行 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com