I’m trying to run a function in an update loop
def get_screencap_raw(self, emulator_id):
# Construct the ADB command to capture a screenshot in raw format
adb_command = [self.adb_path, "-s", emulator_id, "exec-out", "screencap"]
# Directly execute the adb command
process = subprocess.Popen(
adb_command,
stdout=subprocess.PIPE,
startupinfo=self.startupinfo,
)
try:
stdout, stderr = process.communicate(timeout=3)
except subprocess.TimeoutExpired:
process.kill()
return None
finally:
# Ensure the pipes are closed
process.stdout.close()
process.terminate()
if stderr:
print(
f"Error: Unable to capture screenshot for device {emulator_id}. Details: {stderr.decode('utf-8')}"
)
return None
# Return the raw screenshot data
return stdout
But when I continuously run this function, I notice the memory keeps going up and up. I am not sure if my theory is correct, but I believe continuously creating a subprocess.popen, then closing and terminating it is the cause for the memory overhead.
My solution was to try and create an instance of subprocess.popen once, save it in a self variable, and then execute a function that will execute the command for the subprocess. But I have not succeeded in this as it only returns the first output data.
I’d like to know if my theory is correct and if there is a solution to my problem to continuously run a command without creating a new subprocess with every call.