Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make SubprocVecEnv Thread Safe #218

Merged
merged 11 commits into from
Mar 10, 2019
Next Next commit
Use spawn to launch subprocesses
  • Loading branch information
AdamGleave committed Mar 2, 2019
commit a667ada7266647ba645256b8c273266d260ab0f2
9 changes: 6 additions & 3 deletions stable_baselines/common/vec_env/subproc_vec_env.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from multiprocessing import Process, Pipe
import multiprocessing

import numpy as np

@@ -51,9 +51,12 @@ def __init__(self, env_fns):
self.waiting = False
self.closed = False
n_envs = len(env_fns)
self.remotes, self.work_remotes = zip(*[Pipe() for _ in range(n_envs)])
self.processes = [Process(target=_worker, args=(work_remote, remote, CloudpickleWrapper(env_fn)))

ctx = multiprocessing.get_context('spawn') # use thread safe method, see issue #217
self.remotes, self.work_remotes = zip(*[ctx.Pipe() for _ in range(n_envs)])
self.processes = [ctx.Process(target=_worker, args=(work_remote, remote, CloudpickleWrapper(env_fn)))
for (work_remote, remote, env_fn) in zip(self.work_remotes, self.remotes, env_fns)]

for process in self.processes:
process.daemon = True # if the main process crashes, we should not cause things to hang
process.start()