python subprocess become idle after a while

Viewed 333

I have two script, one is for reading serial port continuously and other script is for running the serial port reading script in background. The script given below is I used for running as subprocess.

subprocess.Popen("echo " + user_password + " | sudo -S " + filePath  shell=True, stdout=subprocess.PIPE)

if i run serial port reading script manually, it will not idle, it runs contineously. But if i use the above script for running as subprocess, the serial port reading script become idle after a few minutes. How i can stop this idle issue?

2 Answers

You defined stdout=subprocess.PIPE which allows you to read the output of command by saving them to a buffer memory. But if you don't use/read it, that memory become full after a certain time and subprocess will hang util you use it. You can avoid that issue by passing stdout=subprocess.DEVNULL, or not specifying anything with stdout.

Related