我试图运行一个基于控制台的游戏(地牢爬行石汤 - 用于研究目的自然)多个实例使用多处理池来评估每次运行。使用Python子进程处理交互式shell
在过去,当我使用池来评估相似的代码(遗传算法)时,我用subprocess.call
来分割每个进程。但是,由于dcss具有相当的交互性,因此共享子shell似乎有问题。
我有我通常用于这种事情的代码,用抓取代替我引发GA的其他应用程序。有没有比这更好的方式来处理高度交互的shell?我曾考虑过为每个实例开辟一个屏幕,但认为有一个更清晰的方法。我的理解是shell=True
应该产生一个子外壳,但我想它是在每次调用之间共享的方式产生的。
我应该提到我有一个机器人运行游戏,所以我不希望任何实际的交互从用户的一端发生。
# 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)
这是有帮助的,但我很确定我需要线程池阻塞。它在GA中运行,因此在下一次控制循环迭代被调用之前,必须完成所有群体成员的评估。 – espais
由于for循环中的poll(),它可以被阻塞。这个解决方案的好处是你并不需要自己管理线程/线程池来启动子进程的运行。 – Guillaume