1

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?

Romeo M.
  • 3,218
  • 8
  • 35
  • 40
  • 1
    Have you considered using [pexpect](http://www.noah.org/wiki/pexpect)? – Greg Hewgill Mar 16 '12 at 01:56
  • I agree, pexpect is better suited tool for these kind of tasks – wim Mar 16 '12 at 02:11
  • So I should go for pexpect and wait for `pexpect.EOF` instead right? – Romeo M. Mar 16 '12 at 02:38
  • I don't understand why you can't get the exit status. [`fabric`](http://fabfile.org/) might be simpler to use. – jfs Mar 16 '12 at 06:20
  • because in shell mode the exit status is not the exit status of the process but the one of the shell. Fabric website says that also. – Romeo M. Mar 16 '12 at 17:31
  • Do you have to reuse a single shell? Have you considered using `exec_command()`, which gives a clear indication of an exit code? You would also need not worry about timeouts, and you can get separate stdout and stderr streams. – haridsv May 10 '14 at 02:15
  • can we see the server side? –  Sep 07 '15 at 12:39

0 Answers0