Running stat console commands from a Python script, from the command line

I want to loop over particle systems in the Editor and collect stat gpu information. I have everything working except this line:

unreal.SystemLibrary.execute_console_command(unreal.EditorLevelLibrary.get_editor_world(), “stat gpu”)

The above line works if I manually type it into the editor, in the Python line of the Output Log. It works if I call it from within my .py script, manually imported through the Output Log window.

It will not work if I run it the way I need to run it, from the command line of a DOS shell, like this…

>C:…\UE4Editor-Cmd.exe “C:…\MyGame.uproject” -ExecutePythonScript=“C:…\”

It does not error out. The “stat gpu” command is printed in the log, but the stat gpu command never actually gets successfully executed. I have stepped through the code several times and I can not tell what is going wrong.

How can I run a stat console command from within a python script that is passed into UE4Editor-Cmd.exe on the command line?

Any help appreciated.

It seems like when the code was trying to get the stats I was seeing “audio buffer underrun” errors. Maybe the code is confuised about where it should pull the data from. I also sometimes get warnings about threads without metadata. I am investigating that now.

In OutputDeviceRedirector.cpp, in FOutputDeviceRedirector::InternalFlushThreadedLogs, the BufferedLines is garbage, and then it gets filled with messages saying “Audio Buffer Underrun detected”. I think Unreal is supposed to be using a Stats thread, but instead is using the Game thread. I don’t know much about this stuff, but that’s what it seems like. I am not PIE at the moment.

The above all happens even when stat gpu works correctly.

Note to self: When the stat gpu command is called like this, (which works correctly):

C:\UE4Editor-Cmd.exe “C:\myGame.uproject” -ExecCmds=“stat gpu”

it does a lot of set up work and then runs UEngine::TickDeferredCommands()

When stat gpu is called from within a python script which is called on the DOS command line, (which does not work correctly):

C:\UE4Editor-Cmd.exe “C:\myGame.uproject” -ExecutePythonScript “C:\”

after it does a lot of set up work it then runs UKismetLibrary::ExecuteConsoleCommand()

Could it be that the python script is blocking the Editor until it is finished running, and even though it is running everything “correctly” the Editor shuts down at the end of the python script and we never actually get any stat gpu output?