使用 ray 时在 python 中抑制控制台输出 - SDL 警告

Bob*_*ani 0 python sdl python-3.x ray

我正在使用ray并行运行pygame,但希望如此抑制 SDL 视频警告/错误。

这是我的代码:

@ray.remote
def run2(agent):  # same as run1 

  sys.stdout = open(os.devnull, "w")
  sys.stderr = open(os.devnull, "w") 
  os.environ["SDL_VIDEODRIVER"] = "dummy" 
  env = GameEnv(0)
  state = env.reset()
  steps =0
  done= False
  while not done:
    steps+=1
    action = agent.predict(state)
    ns , r ,done ,i= env.step(env.action_space.sample())
  return steps

# note: maybe run this twice to warmup the system
%time result = ray.get([run2.remote(pops[i]) for i in range(10)])
Run Code Online (Sandbox Code Playgroud)

我曾尝试更改 stdout 和 stderr 但这似乎不起作用,或者我应该在其他地方更改它这是我为每个工人收到的警告

(pid=1128) ALSA lib confmisc.c:767:(parse_card) cannot find card '0'
(pid=1128) ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_card_driver returned error: No such file or directory
(pid=1128) ALSA lib confmisc.c:392:(snd_func_concat) error evaluating strings
(pid=1128) ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_concat returned error: No such file or directory
(pid=1128) ALSA lib confmisc.c:1246:(snd_func_refer) error evaluating name
(pid=1128) ALSA lib conf.c:4528:(_snd_config_evaluate) function snd_func_refer returned error: No such file or directory
(pid=1128) ALSA lib conf.c:5007:(snd_config_expand) Evaluate error: No such file or directory
(pid=1128) ALSA lib pcm.c:2495:(snd_pcm_open_noupdate) Unknown PCM default
Run Code Online (Sandbox Code Playgroud)

有没有办法使用光线参数或全局标准输出块来做到这一点?

Bob*_*ani 5

发现你可以使用:

ray.init(log_to_driver=False)
Run Code Online (Sandbox Code Playgroud)

禁止光线工作者记录输出。

  • @Austin如果将 `logging_level=logging.FATAL` 传递给 `ray.init`,并将 `"log_level": "ERROR"` 添加到代理配置中,则您不应该收到任何与 ray 本身相关的消息。然而,您仍然可能会收到与其他包相关的导入警告。您需要单独禁用它们,例如对 Tensorflow 执行 `tf.get_logger().setLevel('ERROR')`。 (2认同)