The issue is with my code hanging in the for loop of this function. This function is called as a separate thread in a larger main script. After a user input, the main script sets self.kill_switch
.
This code works great when stdout has a continuous output. the issue appears when stdout is empty the code hangs at for line in iter(lambda: stdout.readline(), ""):
until stdout has a line of data. Once stdout has the information it writes the line of data and checks the kill_switch condition and exits according. How to set a timeout on the for line
or check if stdout is empty?
Is there any way to set an if
statement to have the code pass if stdout is blank?
def remote_connection(self):
stdin, stdout, stderr = self.ssh.exec_command('python main_script.py', get_pty=True)
while not self.kill_switch.is_set():
for line in iter(lambda: stdout.readline(), ""):
self.data.put(line)
break
self.ssh.exec_command('\x003', get_pty=True)