我正在尝试使用多进程池运行多个基于控制台的游戏(地牢爬行石汤——自然是为了研究目的),以评估每次运行。过去,当我使用池来评估类似的代码(遗传算法)时,我使用子进程调用(subprocess.call)来分离每个进程。然而,由于dcss非常交互式,共享子shell似乎存在问题。
我有我通常用于此类事情的代码,其中爬行取代了我用GA抛出的其他应用程序。是否有更好的处理高度交互式shell的方法?我曾考虑为每个实例启动一个screen,但认为有一种更清洁的方法。我的理解是,shell=True应该会生成子shell,但我想它可能是以一种在每次调用之间共享的方式生成了一个子shell。
我应该提到,我有一个机器人在运行游戏,因此不希望用户端发生任何实际的交互。
我有我通常用于此类事情的代码,其中爬行取代了我用GA抛出的其他应用程序。是否有更好的处理高度交互式shell的方法?我曾考虑为每个实例启动一个screen,但认为有一种更清洁的方法。我的理解是,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)
poll()
在for循环中使用,它是/可以是阻塞的。这种解决方案的好处在于,你实际上不需要自己管理线程/线程池来启动子进程运行。 - Guillaume