python subprocess communicate() block python subprocess communicate() block python python

python subprocess communicate() block


The whole purpose of the communicate method is to wait for the process to finish and return all the output. If you don't want to wait, don't call communicate. Instead, read from the stdout or stderr attribute to read the output.

If the process outputs to both stdout and stderr (and you want to read it separately), you will have to be careful to actually read from both without blocking, or you can deadlock. This is fairly hard on Windows, and you may wish to use the pexpect module instead.


I faced a similar situation where I had to execute a single command lmstat -a and then get the output of the terminal.

If you just need to run a single command and then read the output, you can use the following code:

import subprocessUsername = 'your_username'Password = 'your_password'IP = 'IP_of_system'Connection_type = '-ssh' #can have values -ssh -telnet -rlogin -raw -serialp = subprocess.Popen(['plink', Connection_type, '-l', Username, '-pw', Password, IP], \                     shell = False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)out, err = p.communicate('lmstat -a\nexit\n'.encode())print(out.decode())


Maybe because "plink.exe" needs to take in input arguments, if you don't pass them, it will block until data are given, you could try adding arguments in method communicate(input)