我已经看过几篇关于这个的帖子,所以我知道这是相当简单的,但我似乎做得不够。我不确定是需要创建工作池还是使用Queue类。基本上,我希望能够创建多个进程,每个进程都是自主的(这就是它们从代理超类继承的原因)。在
在主循环的随机滴答声中,我想更新每个代理。我在主循环和代理的运行循环中使用具有不同值的time.sleep
来模拟不同的处理器速度。在
这是我的代理超类:
# Generic class to handle mpc of each agent
class Agent(mpc.Process):
# initialize agent parameters
def __init__(self,):
# init mpc
mpc.Process.__init__(self)
self.exit = mpc.Event()
# an agent's main loop...generally should be overridden
def run(self):
while not self.exit.is_set():
pass
print "You exited!"
# safely shutdown an agent
def shutdown(self):
print "Shutdown initiated"
self.exit.set()
# safely communicate values to this agent
def communicate(self,value):
print value
特定代理的子类(模拟HVAC系统):
^{pr2}$我的主循环:
if __name__ == "__main__":
print "Initializing subsystems"
agents = {}
agents['HVAC'] = HVAC()
# Run simulation
timestep = 0
while timestep < args.timesteps:
print "Timestep %d" % timestep
if timestep % 10 == 0:
curr_temp = random.randrange(68,72)
curr_humidity = random.uniform(40.0,60.0)
agents['HVAC'].communicate({'temp':curr_temp, 'humidity':curr_humidity})
time.sleep(1)
timestep += 1
agents['HVAC'].shutdown()
print "HVAC process state: %d" % agents['HVAC'].is_alive()
所以问题是,每当我在主循环中运行agents['HVAC'].communicate(x)
时,我都可以看到值被传递到其run
循环中的HVAC
子类中(因此它正确地打印接收到的值)。但是,该值从未成功存储。在
所以典型的输出如下:
Initializing subsystems
Timestep 0
Measured [68] [56.948675]
heating 1
heating 2
Timestep 1
heating 3
heating 4
Timestep 2
heating 5
heating 6
实际上,一旦出现测量值[68],内部存储值应更新为输出68(不是加热1、加热2等)。如此有效地,暖通空调的自身温度没有得到适当的更新。在
编辑:经过一番研究,我意识到我并不一定了解幕后发生了什么。每个子进程都使用自己的虚拟内存块来运行,并且完全从这种方式共享的任何数据中抽象出来,因此传入值是行不通的。我的新问题是,我不一定确定如何与多个进程共享全局值。在
我正在查看Queue或JoinableQueue包,但我不一定确定如何将队列传递到我拥有的超类设置类型中(尤其是使用mpc.Process.__init__(self)
调用)。在
另一个问题是,我是否可以让多个代理从队列中读取值而不将其从队列中拉出?例如,如果我想与多个代理共享一个temperature
值,那么一个队列是否可以用于此目的?在
以下是一个建议的解决方案,假设您希望:
在我展示之前,我想说的是,一般情况下,除非您是CPU专用的,否则{}实际上并不适合,主要是因为增加了复杂性,而且您最好使用不同的高级异步框架。另外,你应该使用python3,它好得多!在
也就是说,^{} ,使用
multiprocessing
可以很容易地实现这一点。我已经在Python3中完成了这项工作,但我不认为任何东西在Python2中不应该“只工作”,但我还没有检查过。在相关问题 更多 >
编程相关推荐