Skip to content

Instantly share code, notes, and snippets.

@denizs
Created Jul 11, 2017
Embed
What would you like to do?
Openai gym `render(mode='rgb_array')` crashes when executed within `multiprocessing.Process`
import gym
from multiprocessing import Process
class Worker(Process):
def __init__(self, env_name, name=None, render_rgb=False):
Process.__init__(self, name=name)
self.env = gym.make(env_name)
self.env.reset()
self.render_rgb = render_rgb
print('Environment initialized. {}'.format(self.name))
def run(self, render_rgb=False):
for _ in range(100):
action = self.env.action_space.sample()
obs, reward, done, _ = self.env.step(action)
print(obs)
if self.render_rgb:
observation = self.env.render(mode='rgb_array')
if done:
print('Done with an epsidode for {}'.format(self.name))
self.env.reset()
class Agent(object):
def __init__(self, env_name, num_env, render_rgb=False):
assert num_env > 0, 'Number of environments must be postive.'
self.num_env = num_env
self.workers = []
for env_idx in range(num_env):
env_worker = Worker(env_name, name=str(env_idx),
render_rgb=render_rgb)
self.workers.append(env_worker)
for w in self.workers:
w.start()
for w in self.workers:
w.join()
print('Initialization without render')
p_env = Agent('MountainCar-v0', 4, render_rgb=False)
print('Initialization with render')
p_env = Agent('MountainCar-v0', 4, render_rgb=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment