【发布时间】:2017-09-19 03:43:23
【问题描述】:
我正在尝试使用多处理池运行基于控制台的游戏的多个实例(地牢爬行石汤——自然是出于研究目的)来评估每次运行。
过去,当我使用池来评估类似代码(遗传算法)时,我使用subprocess.call 来拆分每个进程。然而,由于 dcss 的交互性很强,共享子 shell 似乎是有问题的。
我有我通常用于这种事情的代码,用 crawl 替换了我抛出 GA 的其他应用程序。还有比这更好的方法来处理高度交互的 shell 吗?我曾考虑为每个实例启动一个屏幕,但认为有一种更清洁的方法。我的理解是shell=True 应该生成一个子shell,但我想我是在以每次调用之间共享的方式生成一个子shell。
我应该提到我有一个运行游戏的机器人,所以我不希望发生来自用户端的任何实际交互。
# Kick off the GA execution
pool_args = zip(trial_ids,run_types,self.__population)
pool.map(self._GAExecute, pool_args)
---
# called by pool.map
def _GAExecute(self,pool_args):
trial_id = pool_args[0]
run_type = pool_args[1]
genome = pool_args[2]
self._RunSimulation(trial_id)
# Call the actual binary
def _RunSimulation(self, trial_id):
command = "./%s" % self.__crawl_binary
name = "-name %s" % trial_id
rc = "-rc %s" % os.path.join(self.__output_dir,'qw-%s'%trial_id,"qw -%s.rc"%trial_id)
seed = "-seed %d" % self.__seed
cdir = "-dir %s" % os.path.join(self.__output_dir,'qw-%s'%trial_id)
shell_command = "%s %s %s %s %s" % (command,name,rc,seed,cdir)
call(shell_command, shell=True)
【问题讨论】:
标签: python python-2.7 shell subprocess python-multiprocessing