Hi, my old thread got closed since I got nauseous before I could properly try for a solution so I'm reposting. If the same happens yet again I don't know what to do really.
The old thread can hopefully found here #1270333189172367394 message but I'll reiterate everything for clarity
Current code
def _runScript(self,scriptloc:pathlib.Path):
self.logger.debug(f"Running script at location: {scriptloc}")
processAction=[]
self._assignRunCommand(scriptloc, processAction)
processAction.append(str(scriptloc))
try:
process=subprocess.Popen(["./"+scriptloc], stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)
stdout_lines = []
stderr_lines = []
while True:
output=''
self.logger.debug("inside loop, reading output")
output=process.stdout.readline().decode('utf-8').strip()
if output != '':
self.logger.info(f"stdout: {output}")
stdout_lines.append(output)
errors=process.stderr.readline().decode('utf-8').strip()
if errors != '':
self.logger.info(f"stderr: {errors}")
stderr_lines.append(errors)
if output == '' and process.poll() is not None:
self.logger.debug("output is empty and process is done")
break
return {
'stdout': '\n'.join(stdout_lines),
'stderr': '\n'.join(stderr_lines),
'returncode': process.returncode
}
except Exception as e:
self.logger.error("Error in running script! Error message is as follows\n"+str(e))
return (None)
What is it supposed to do
The function should run an arbitrary bash script at scriptloc, and return its output to stdout and stderr as well as print any and all prints the script being run has in real time as the script is running.
What is it actually doing
The function returns correctly in every attempted case. The problem is the printing part. When the script is a short quick one there are no issues, but if it takes some amount of time - for instance my test script echos the numbers 0 through 9 over a period of ten seconds - the printing fails. Instead of seeing a number every second, the program prints 0, then seemingly halts, until after about ten seconds every remaining print is shown at once, like so (note the timestamps):
2024-08-07 11:12:41,733 - Task - delay - Running test "delay"
2024-08-07 11:12:41,738 - Task - delay - stdout: 0
2024-08-07 11:12:51,760 - Task - delay - stdout: 1
2024-08-07 11:12:51,760 - Task - delay - stdout: 2
2024-08-07 11:12:51,760 - Task - delay - stdout: 3
2024-08-07 11:12:51,760 - Task - delay - stdout: 4
2024-08-07 11:12:51,760 - Task - delay - stdout: 5
2024-08-07 11:12:51,760 - Task - delay - stdout: 6
2024-08-07 11:12:51,761 - Task - delay - stdout: 7
2024-08-07 11:12:51,761 - Task - delay - stdout: 8
2024-08-07 11:12:51,761 - Task - delay - stdout: 9