473,394 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Subprocess not working with stdin/stdout set to PIPE

I am trying to use the subprocess module to run an interactive tool, issue some commands via stdin, then read the results from stdout and end.

So I do the following:

Expand|Select|Wrap|Line Numbers
  1. output = Popen(["mycmd", "myarg"], stdin=PIPE,stdout=PIPE).communicate('prompt command')[0]
  2.  
mycmd should load, write output to stdout, then prompt for data. When data is given it prints some more. Well when I call it the prompt command never gets called and output contains only the init prints. If I set stdout to None, the prompt command runs and everything prints out as intended. In this case its printed to the screen, not to the output.

So why does it break when I try to pipe the output? I am using Python 2.6.2.
Jul 24 '10 #1
3 5965
dwblas
626 Expert 512MB
You are missing a comma after stdout=PIPE for starters. Also, you may have to flush stdout. There aren't any write statements but I assume that is in some other part of your code. With just the one line there is very little to be done. First, see Doug Hellmann's examples here http://blog.doughellmann.com/2007/07...ubprocess.html. Reading and writing is about 1/3 of the way down the page. Second, if you want further help, come up with a simple program that reads and writes from a list (or that is supposed to read and write) and post it here so there is some specific code to help with.
Jul 24 '10 #2
Not sure I see the need for a trailing comma, its the last param so no need for it. And the program does run, just not correctly.

I tried to simplify my code and likely did too much. So here is my code.

Expand|Select|Wrap|Line Numbers
  1. proc = subprocess.Popen('myprogram',
  2.                        stdin=subprocess.PIPE,
  3.                        stdout=subprocess.PIPE
  4.                        )
  5. (stdoutdata, stderrdata) = proc.communicate('print hello')
  6. print str(stdoutdata)
  7.  
When this runs the print does print out the programs startup messages (just some diag prints about loading config files, etc. which takes about 15-20secs) and that's all. The subprocess hould also be creating its own log files but nothing is in there, its as if it loads the program then exits without applying code to the stdif. If I do the following:

Expand|Select|Wrap|Line Numbers
  1. proc = subprocess.Popen('myprogram',
  2.                        stdin=None,
  3.                        stdout=subprocess.PIPE
  4.                        )
  5. (stdoutdata, stderrdata) = proc.communicate('print hello')
  6. print str(stdoutdata)
  7.  
I see the startup messages, plus the output of running he 'print hello' input to the prompt and everything is as expected including the subprocess logs. Why does it not work when I redirect to a variable?
Jul 24 '10 #3
Sorry copied the wrong test code for the second example, that works. The code that acts as it should is

Expand|Select|Wrap|Line Numbers
  1. proc = subprocess.Popen('myprogram',
  2.                         stdin=subprocess.PIPE,
  3.                         stdout=None
  4.                         )
  5. (stdoutdata, stderrdata) = proc.communicate('print hello')
  6. print str(stdoutdata)
That is stdout is None not stdin, which of course would not work since stdin would not do anything.
Jul 24 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Jean-Pierre Bergamin | last post by:
Dear python-Community We are forced to use a quite old simulation software that's based on Modula-2. The idea is now to let this software "talk" to the outside world over a TCP/IP network. ...
1
by: Vincent Touquet | last post by:
Hi, In a project where I have embedded Python in a C++ application, I have the need to replace what Python considers to be stdin, stdout and/or stderr. In sysmodule.c in the Python sources, I...
1
by: Harry George | last post by:
Normally the SOAP Servers are designed to take control of a port and run their own sockets via inheritance from SocktServer. But under inetd and xinetd, the port is controlled elsewhere and the...
1
by: Michael McGarry | last post by:
Hi, How do I redirect stdin, stdout and stderr to a window? I am using Qt for GUI. Thanks, Michael
5
by: Michael McGarry | last post by:
Hi, How can I redirect stdin/stdout to GUI widgets? Michael
3
by: Mike Finister | last post by:
Hi there At the moment I am writing a GUI front-end that in the background is to call some scripts on a Unix box. The GUI has to be written in Visual Basic (yes I said the VB word...sorry! :-) )...
5
by: Martijn Brouwer | last post by:
I am writing a unix daemon in python, so I want to close stdin, stdout and stderr. My first attempt was to the standard file descriptors using their close() methods. After closing stdout, I could...
2
momotaro
by: momotaro | last post by:
Hello! am wondring if stdin, stdout and stderr are of any help since we can do the same thing using 'printf' for example: fprintf(stdout, "%s", message); is exactely the same as: ...
6
by: gregpinero | last post by:
Let's say I have this Python file called loop.py: import sys print 'hi' sys.stdout.flush() while 1: pass And I want to call it from another Python process and read the value 'hi'. How...
8
by: subramanian100in | last post by:
The file stdio.h just contains extern FILE *stdin; extern FILE *stdout; extern FILE *stderr; When are the standard streams stdin, stdout, stderr initialiazed ? How are they initialiazed ? ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.