Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python subprocess and user interaction

I'm working on a GUI front end in Python 2.6 and usually it's fairly simple: you use subprocess.call() or subprocess.Popen() to issue the command and wait for it to finish or react to an error. What do you do if you have a program that stops and waits for user interaction? For example, the program might stop and ask the user for an ID and password or how to handle an error?

c:\> parrot Military Macaw - OK Sun Conure - OK African Grey - OK Norwegian Blue - Customer complaint! (r) he's Resting, (h) [Hit cage] he moved, (p) he's Pining for the fjords 

So far everything I've read tells you how to read all output from a program only after it's finished, not how to deal with output while the program is still running. I can't install new modules (this is for a LiveCD) and I'll be dealing with user input more than once.

like image 939
Dave Brunker Avatar asked Jan 22 '13 11:01

Dave Brunker


People also ask

How do you interact with a process in Python?

Passing Messages to Processes A simple way to communicate between process with multiprocessing is to use a Queue to pass messages back and forth. Any pickle-able object can pass through a Queue. This short example only passes a single message to a single worker, then the main process waits for the worker to finish.

What is the use of subprocess in Python?

Subprocess in Python is a module used to run new codes and applications by creating new processes. It lets you start new applications right from the Python program you are currently writing. So, if you want to run external programs from a git repository or codes from C or C++ programs, you can use subprocess in Python.

What is Popen Python?

Description. Python method popen() opens a pipe to or from command. The return value is an open file object connected to the pipe, which can be read or written depending on whether mode is 'r' (default) or 'w'. The bufsize argument has the same meaning as in open() function.

What is the difference between subprocess call and run?

I can say that you use subprocess. call() when you want the program to wait for the process to complete before moving onto the next process. In the case of subprocess. run() , the program will attempt to run all the processes at once, inevitably causing the program to crash.


1 Answers

Check out the subprocess manual. You have options with subprocess to be able to redirect the stdin, stdout, and stderr of the process you're calling to your own.

from subprocess import Popen, PIPE, STDOUT  p = Popen(['grep', 'f'], stdout=PIPE, stdin=PIPE, stderr=STDOUT)  grep_stdout = p.communicate(input='one\ntwo\nthree\nfour\nfive\nsix\n')[0] print grep_stdout 

You can also interact with a process line by line. Given this as prog.py:

import sys print 'what is your name?' sys.stdout.flush() name = raw_input() print 'your name is ' + name sys.stdout.flush() 

You can interact with it line by line via:

>>> from subprocess import Popen, PIPE, STDOUT >>> p = Popen(['python', 'prog.py'], stdout=PIPE, stdin=PIPE, stderr=STDOUT) >>> p.stdout.readline().rstrip() 'what is your name' >>> p.communicate('mike')[0].rstrip() 'your name is mike' 

EDIT: In python3, it needs to be 'mike'.encode().

like image 148
Mike Avatar answered Oct 03 '22 00:10

Mike