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

Suppress console output in python when using ray - SDL warnings

我运行一个pygame并行使用ray,但是想要这样抑制SDL视频warnings/errors.

这是我的代码:

@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)])

我试过更改 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

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

发现你可以使用:

ray.init(log_to_driver=False)

禁用 ray worker 记录输出。

如果你在 UNIX 下,

2>/dev/null 对我也有用,但它有点激进。印刷品仍将在工人内部工作。

所以:python script.py 2>/dev/null