1

I have this little utility function that I use to execute external processes. In some cases, it hangs when I try to read the output of the process. Commenting out the output reading lines, it works.

def execute(command, cwd):
    command = command.split(' ')
    process = Popen(command,
        stderr=STDOUT, stdout=PIPE,
        cwd = cwd)

    # WITHOUT THIS IT WORKS
    #for line in process.stdout:    
    #    log.info('executing %s for user %s: %s' % (command, user.username, line))

    # ANOTHER TRY AT READING THE OUTPUT. ALSO MAKES IT HANG
    #output = p.communicate()[0]
    #log.info('executing %s for user %s: %s' % (command, user.username, output))

    process.wait()

Any help? I need to output for debugging purpose.

pistacchio
  • 56,889
  • 107
  • 278
  • 420
  • 2
    What is the command you are trying to execute? Also, to parse a command, use `shlex.split(command)` to ensure handling of quotes and such. – Martijn Pieters Sep 25 '12 at 08:03
  • see http://stackoverflow.com/questions/2804543/read-subprocess-stdout-line-by-line – georg Sep 25 '12 at 09:11

1 Answers1

2

I started using async_subprocess years ago and never looked back. It's also available from PyPI here.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55