I'm using paramiko to compile a program on a server. As you may know it compiling takes a lot of time and a lot of data is being sent out from this process. Since I cannot get from a shell command the exit status of the command to be executed I'm forced to wait for the data to be sent out to me and, when it stops sending, I can move on to the next command in line.
This practice is not reliable because of some issues:
- the program may still execute without sending output
- I have to use sleep commands if recv_ready
returns false in order to keep my memory low
- using sleep commands is time consuming given the fact that I don't know how much to "sleep" it before considering the process as done so I can move on to the next one
Here's my actual code so far:
for command in commands:
shell.send( "%s\n" % command )
waited = 0
while True:
if shell.recv_ready():
x = shell.recv( 1024 )
if len( x ):
print x
shell.settimeout( 1 )
waited = 0
else:
time.sleep( 1 )
waited += 1
if waited > 10:
break
This is working fine for most cases but if the running task lasts longer than 10 seconds my program moves to the next command because he thinks it's done. If I increase the timeout threshold to say 20 seconds I lose 20 seconds between each command being executed which is a lot of time wasted considering the fact that most commands take less than 1-2 seconds to execute.
How do you guys suggest i should do this?