0

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)
curlpipesudobash
  • 691
  • 1
  • 10
  • 21
gvega
  • 11
  • 4
  • Take a look at: https://stackoverflow.com/questions/31834743/get-output-from-a-paramiko-ssh-exec-command-continuously Similar use case – seayak Nov 16 '18 at 23:40
  • Thanks. I looked at that case previously and implemented the second comment. Adding "get_pty=True" allowed me to read one line then proceed with the code. However, the code still stalls until it has a line of code to output. i.e. the for loop appears to still require some output before it will proceed and will not just return an empty line. – gvega Nov 16 '18 at 23:50
  • Suppose that stdout is going to receive the line `hello\n` in ten minutes. What would you have `stdout.readline()` return *now*, before the ten minutes expire? – torek Nov 17 '18 at 02:41
  • Ideally it would not receive anything and just `pass` when there is no output in stdout. My thought is this would allow the code to continue and check the switch. Using this method I would simply use much shorter timeouts to check the stdout more frequently. – gvega Nov 18 '18 at 22:23

0 Answers0