Python check_output method does not return result sometimes

I have a Python script that has to run a large number of other scripts, each of which is located in a subdirectory of the script working directory. It is assumed that each of these scenarios connects to the game client and launches the AI ​​for this game. To complete this run, I had to run each script in two separate threads (one for each player). The problem I am facing is that sometimes the output of the scripts is not recorded. My startup code looks like this:

def run(command, name, count): chdir(name) output = check_output(" ".join(command), stderr = STDOUT, shell = True).split('\r') chdir('..') with open("results_" + str(count) + ".txt", "w") as f: for line in output: f.write(line) 

The strange part is that it manages to capture longer streams, but short ones go unnoticed. How can I change my code to fix this problem?

UPDATE: I do not think this is a buffering problem, because check_output("ls ..", shell = True).split('\n')[:-1] returns the expected result, and this command takes much less time. than the scripts I'm trying to run.

UPDATE 2: I found that the output is reduced for longer runs. It turns out that the end of the output is skipped for all the processes that I run for some reason. This also explains why shorter runs do not produce any output at all.

+5
source share

Source: https://habr.com/ru/post/1258230/


All Articles