There seem to be a lot of ways to invoke some system process via the command line using python, and then to read the output once the process is complete. For example, the subprocess
module can accomplish this via subprocess.Popen(['ls'])
.
Is there a way to read the output from a command while it is still running?
For example, if I invoke the python script multiply.py
:
import time
def multiply(a,b):
newA = a
newB = b
while True: #endless loop
newA = newA+1
newB = newB+1
product = newA * newB
print 'Product: ',product
time.sleep(1)
multiply(40,2)
using something along the lines of subprocess.Popen(['python', 'multiply.py])
, is there a way to
- Start the process
- Silently/actively capture all the output while the process is running
- 'jump in' at any time to check the contents of the entire output?
The above python script is a model of the sort of process from which I'm interested in capturing output, i.e. there is an endless loop, which prints an output every second; it is this output that I'm interested in actively monitoring/capturing.