pysc2 icon indicating copy to clipboard operation
pysc2 copied to clipboard

Redirecting stderr of SC2 process

Open inoryy opened this issue 6 years ago • 3 comments

Currently there's no way to redirect SC2 proc stderr (i.e. Game has started. messages) except for doing it globally, which is quite annoying if you're running multiple SC2 env instances.

The way it should be done from within Python is by allowing stderr=... parameter in the subprocess.Popen call during StarcraftProcess::_launch call.
One way to set is could be a global flag since project relies on gflags already.

inoryy avatar Oct 21 '17 12:10 inoryy

Do I understand this as a request to be able to redirect the stderr from the game to a file, and to do so via a global flag? How would that help when you're running multiple instances? Wouldn't it send the stderr from all instances to that same file?

tewalds avatar Nov 16 '17 00:11 tewalds

Yes, it is really annoying. For me , I just want to hide all output from SC2 proc and keep output of my program. Do not how to do this!!!

world2vec avatar Dec 20 '17 02:12 world2vec

I'll look into fixing this in the near future, but a simple workaround I've been using so far is to redirect all stderr somewhere, i.e. python main.py 2> logs/errors.log

inoryy avatar Feb 12 '18 07:02 inoryy

Unfortunately, this behavior is still the same in today's version.

I suggest updating the method StarcraftProcess()._launch() that already receives **kwargs but does not make use of it.

    def _launch(self, run_config, args, **kwargs):
        """Launch the process and return the process object."""
        # del kwargs
        try:
            with sw("popen"):
                return subprocess.Popen(args, cwd=run_config.cwd, env=run_config.env, **kwargs)
        except OSError:
            logging.exception("Failed to launch")
            raise SC2LaunchError("Failed to launch: %s" % args)

In this way, one could send something like stderr=subprocess.PIPE to ridirect the output of stderr when calling the function LocalBase().start(), e.g.,

from pysc2 import run_configs

my_run_config = run_configs.get(version=<some_game_version>)
# just drop stderr in this example
sc2_proc = my_run_config.start(want_rgb=False, stderr=subprocess.DEVNULL)

Joy1112 avatar Apr 19 '23 13:04 Joy1112

Hah, I happened to be pulling in PRs for the first time in ages, and saw this comment. Thanks for the suggestion.

tewalds avatar Apr 19 '23 16:04 tewalds