0

I've been working on some code that uses subprocess.checkoutput to execute commands on a remote server through ssh:

   subprocess.check_output(['ssh', '-o', 'StrictHostKeyChecking=no',
        '-i', 'key.pem', self.pDNS,
        'python script.py', html.replace('&', '\&'), '"
        {0}"'.format(ua.random)], stderr=subprocess.STDOUT)

This command is run by a number of threads communicating with different servers with little to no shared resources and works fine as far as I've noticed.

However, on top of this code, I've used curses to write a rudimentary UI for me to monitor what my code is doing. I'm able to input commands using keyboard in a way similar to VIM or EMACS. However, I've noticed that when my code is executing the subprocess line above, keyboard input becomes very unreliable and sporadic, requiring make many repeated key presses for it to register in the program.

Is this because of some issues with subprocess playing nice with the threading library? If not, does anyone know what might be causing this problem?

Frank Wang
  • 181
  • 9
  • 2
    you should explicitly separate input of the main process and subprocesses by specifying parameter `stdin=subprocess.DEVNULL` (or `stdin=subprocess.PIPE` for processes demanding input) – user3159253 May 26 '16 at 01:40
  • 1
    update: `subprocess.DEVNULL` was introduced in python-3.3, so, likely, `PIPE` is the only option for earlier versions. – user3159253 May 26 '16 at 01:43
  • Thanks, that suggest works. – Frank Wang May 26 '16 at 02:00
  • unrelated: 1- you should pass the remote command as a single string e.g., `'/usr/bin/python -c "import sys; print(sys.argv)" a "b c"'`. 2- if you don't need the output; use `check_call(cmd, stdin=DEVNULL,stdout=DEVNULL, stderr=STDOUT, close_fds=True)` 3- use `pipes.quote(html)`, to escape shell-metacharacters – jfs May 26 '16 at 05:45
  • 1
    @user3159253: it is simple to [emulate `subprocess.DEVNULL` on earlier Python versions `DEVNULL = open(os.devnull, 'r+b', 0)`](http://stackoverflow.com/q/11269575/4279) – jfs May 26 '16 at 05:46

0 Answers0