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

Parallelization of this "CartPole v0" standard environment failed #905

Open
fengyuhun opened this issue Dec 16, 2023 · 0 comments
Open

Parallelization of this "CartPole v0" standard environment failed #905

fengyuhun opened this issue Dec 16, 2023 · 0 comments

Comments

@fengyuhun
Copy link

import tensorflow as tf
from tf_agents.environments import suite_gym
from tf_agents.environments import tf_py_environment
from tf_agents.environments import parallel_py_environment

创建单个CartPole环境

def get_CartPole_Env():
return suite_gym.load("CartPole-v0")

创建并行环境

tf_env = tf_py_environment.TFPyEnvironment(
parallel_py_environment.ParallelPyEnvironment([get_CartPole_Env] * 4)
)

RuntimeError Traceback (most recent call last)
in <cell line: 11>()
10 # 创建并行环境
11 tf_env = tf_py_environment.TFPyEnvironment(
---> 12 parallel_py_environment.ParallelPyEnvironment([get_CartPole_Env] * 4)
13 )

6 frames
/usr/local/lib/python3.10/dist-packages/tf_agents/system/system_multiprocessing.py in get_context(method)
85 """
86 if not multiprocessing_core.initialized():
---> 87 raise RuntimeError(_NOT_INITIALIZED_ERROR)
88 return _rewrite_target_with_state(multiprocessing_core.get_context(method))
89

RuntimeError: Unable to load multiprocessing context.

Please ensure that you properly initialize your program by wrapping your main()
call:

def main(argv):
...

if name == 'main':
tf_agents.system.multiprocessing.handle_main(main, extra_state_savers=...)

or, if using absl.app:

if name == 'main':
tf_agents.system.multiprocessing.handle_main(
functools.partial(absl.app.run, main), extra_state_savers=...)

For unit tests, this also means wrapping your test.main using handle_test_main:

if name == 'main':
tf_agents.system.multiprocessing.handle_test_main(
tf.test.main, extra_state_savers=...)

or

if name == 'main':
tf_agents.system.multiprocessing.handle_test_main(
tf_agents.utils.test_utils.main, extra_state_savers=...)

If you are in interactive mode (e.g. python console, ipython, jupyter notebook)
use:

tf_agents.system.multiprocessing.enable_interactive_mode(
extra_state_savers=...)

For more details on state savers, see the docstrings for
tf_agents.multiprocessing.handle_* and:

https://pythonspeed.com/articles/python-multiprocessing/

In call to configurable 'ParallelPyEnvironment' (<class 'tf_agents.environments.parallel_py_environment.ParallelPyEnvironment'>)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant