0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

pygletをmulti threadで実行するとexceptionとなる場合はmulti processを使うと良い

Posted at

Background

強化学習でエピソードを蓄えるために、OpenAI GymのCarRacing-v0をmulti threadを用いて実行しようとしたところexceptionが発生してしまった。
Exceptionの内容はこちら。CarRacing-v0のwindowを準備するためのpygletがマルチスレッドで実行できない模様。

Exception has occurred: RuntimeError
EventLoop.run() must be called from the same thread that imports pyglet.app

Multi process

いろいろ調てみてもmulti threadでpygletを実行する方法が見つからなかったが、代わりにmulti processを使ってみたところすんなり実行できた。

Pythonではmultiprocessingで実現でき、使い方はほぼthreadingと一緒。

パラメータ受け渡しについて

私はmain processでGPUを用いてagentのtrainingに専念させて、sub processでエピソードを生成するようにしていた。(Sub processはdaemonとしておく)
Main processで学習したパラメータはPythonのmultiprocessingのドキュメントで勧められている通りQueueを使ってsub processに渡していたが、tensorを直接渡すようにしているとmain processを終了させてもdaemonにしたはずのsub processが終了されなくなってしまった。
Parameterを渡す際はnumpy arrayなどに変換して渡してあげると、daemon processもmain processと一緒に終了してくれるようになった。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?