Я пытаюсь реализовать многопроцессорную работу с OpenAI Gym.Я знаю, что есть готовые решения, но я делаю это для того, чтобы немного попрактиковаться как в Gym, так и в многопроцессорной обработке.
Проблема в том, что я запускаю ошибку, причину которой я не могу определить.Вот код:
import gym
import numpy as np
import multiprocessing as multip
class Environments:
gym_environment = 'CarRacing-v0'
def __init__(self, environments_n):
self.environments_n = environments_n
self.cores_count = multip.cpu_count()
self.envs = []
self.reset_all_environments()
def __enter__(self):
self.pool = multip.Pool(self.cores_count)
return self
def __exit__(self, exc_type, exc_value, tb):
self.pool.close()
self.pool.join()
def reset_all_environments(self):
for env in self.envs:
env.close()
self.envs = [gym.make(self.gym_environment) for _ in range(self.environments_n)]
self.dones = [False for _ in range(self.environments_n)]
self.last_obs = [env.reset() for env in self.envs]
@staticmethod
def step_single(env, action):
print(action, env)
return env.step(action)
def step_all(self):
actions = [env.action_space.sample() for env in self.envs]
results = self.pool.starmap(self.step_single, zip(self.envs, actions))
if __name__ == '__main__':
with Environments(2) as envs:
print(envs.step_all())
В моем ожидании это должно просто выполнить один шаг в 2 созданных средах, однако вместо этого я получаю эту ошибку:
Track generation: 1134..1423 -> 289-tiles track
retry to generate track (normal if there are not many of this messages)
Track generation: 1153..1445 -> 292-tiles track
Track generation: 1188..1492 -> 304-tiles track
retry to generate track (normal if there are not many of this messages)
Track generation: 1233..1554 -> 321-tiles track
[0.01238655 0.20499504 0.07908794] <TimeLimit<CarRacing instance>>
[0.8294716 0.5289044 0.7956768] <TimeLimit<CarRacing instance>>
multiprocessing.pool.RemoteTraceback:
"""
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\multiprocessing\pool.py", line 121, in worker
result = (True, func(*args, **kwds))
File "C:\Program Files\Python37\lib\multiprocessing\pool.py", line 47, in starmapstar
return list(itertools.starmap(args[0], args[1]))
File "C:\Users\Nadni\PycharmProjects\ActorCritic\test.py", line 33, in step_single
return env.step(action)
File "C:\Program Files\Python37\lib\site-packages\gym\wrappers\time_limit.py", line 15, in step
observation, reward, done, info = self.env.step(action)
File "C:\Program Files\Python37\lib\site-packages\gym\envs\box2d\car_racing.py", line 315, in step
self.car.steer(-action[0])
AttributeError: 'NoneType' object has no attribute 'steer'
"""
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "test.py", line 42, in <module>
print(envs.step_all())
File "test.py", line 37, in step_all
results = self.pool.starmap(self.step_single, zip(self.envs, actions))
File "C:\Program Files\Python37\lib\multiprocessing\pool.py", line 276, in starmap
return self._map_async(func, iterable, starmapstar, chunksize).get()
File "C:\Program Files\Python37\lib\multiprocessing\pool.py", line 657, in get
raise self._value
AttributeError: 'NoneType' object has no attribute 'steer'
Exception ignored in: <function Viewer.__del__ at 0x000002DCC71E8A60>
Traceback (most recent call last):
File "C:\Program Files\Python37\lib\site-packages\gym\envs\classic_control\rendering.py", line 152, in __del__
File "C:\Program Files\Python37\lib\site-packages\gym\envs\classic_control\rendering.py", line 71, in close
File "C:\Program Files\Python37\lib\site-packages\pyglet\window\win32\__init__.py", line 305, in close
File "C:\Program Files\Python37\lib\site-packages\pyglet\window\__init__.py", line 770, in close
ImportError: sys.meta_path is None, Python is likely shutting down
И я понятия не имеюпочему он говорит AttributeError: 'NoneType' object has no attribute 'steer'
, хотя из print () ясно, что переменная env
правильно является членом <TimeLimit<CarRacing instance>>