473,385 Members | 1,409 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,385 software developers and data experts.

subprocess communication, exec()

If I run 'python -i subprocessclient.py' I expect to see the nice
level of it go up 2, and the nice level of the subprocess go up 1.
But all I see is the nice level of the client change. What am I doing
wrong?

subprocessserver.py:
----------------------------
#!/usr/bin/python2.5

import os
import sys

while True:
next_line = sys.stdin.readline()
if not next_line:
break
exec(next_line)
# sys.stdout.write(output)
# sys.stdout.write(next_line)
# sys.stdout.flush()
----------------------------

subprocessclient.py:
----------------------------
#!/usr/bin/python2.5

import subprocess, os

server = subprocess.Popen(('python2.5', 'subprocessserver.py'),
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

os.nice(2)

server.stdin.write('''os.nice(1)''')
----------------------------

Thanks.
-Chuckk

--
http://www.badmuthahubbard.com
Nov 11 '08 #1
2 2972
On Nov 11, 1:23 pm, "Chuckk Hubbard" <badmuthahubb...@gmail.com>
wrote:
If I run 'python -i subprocessclient.py' I expect to see the nice
level of it go up 2, and the nice level of the subprocess go up 1.
But all I see is the nice level of the client change. What am I doing
wrong?

subprocessserver.py:
----------------------------
#!/usr/bin/python2.5

import os
import sys

while True:
next_line = sys.stdin.readline()
if not next_line:
break
exec(next_line)
# sys.stdout.write(output)
# sys.stdout.write(next_line)
# sys.stdout.flush()
----------------------------

subprocessclient.py:
----------------------------
#!/usr/bin/python2.5

import subprocess, os

server = subprocess.Popen(('python2.5', 'subprocessserver.py'),
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

os.nice(2)

server.stdin.write('''os.nice(1)''')
----------------------------

Thanks.
-Chuckk

--http://www.badmuthahubbard.com
Looks like you're dropping an error by redirecting the child process'
standard error into a pipe.

First off, remove the stderr=subprocess.PIPE from
subprocessclient.py. When you do so, you'll start seeing an error
message whenever your code runs:

[jeff@marvin ~]$ python client.py
None
[jeff@marvin ~]$ Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined

That doesn't make a lot of sense at first, until you consider the
following:

[jeff@marvin ~]$ echo 'os.nice(1)' | python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined

The Python interpreter is trying to execute the data you write to the
stdin of the child process. Changing the Popen line to use the string
form rather than the sequence form remedies the problem, as does
changing 'string=True' to 'string=False.'

The reason? Because when you set shell=True, additional arguments in
args sequence are passed as additional arguments to the shell itself,
not to the command.

From subprocess.py:

if isinstance(args, types.StringTypes):
args = [args]
else:
args = list(args)

if shell:
args = ["/bin/sh", "-c"] + args

So, in your attempt, you're effectively doing the following:

/bin/sh -c "python2.5" "server.py"

When you want:

/bin/sh -c "python2.5 server.py"

HTH,

Jeff
Nov 11 '08 #2
On Tue, Nov 11, 2008 at 9:39 PM, Jeff McNeil <je**@jmcneil.netwrote:
On Nov 11, 1:23 pm, "Chuckk Hubbard" <badmuthahubb...@gmail.com>
wrote:
>If I run 'python -i subprocessclient.py' I expect to see the nice
level of it go up 2, and the nice level of the subprocess go up 1.
But all I see is the nice level of the client change. What am I doing
wrong?

subprocessserver.py:
----------------------------
#!/usr/bin/python2.5

import os
import sys

while True:
next_line = sys.stdin.readline()
if not next_line:
break
exec(next_line)
# sys.stdout.write(output)
# sys.stdout.write(next_line)
# sys.stdout.flush()
----------------------------

subprocessclient.py:
----------------------------
#!/usr/bin/python2.5

import subprocess, os

server = subprocess.Popen(('python2.5', 'subprocessserver.py'),
shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE,
stderr=subprocess.PIPE)

os.nice(2)

server.stdin.write('''os.nice(1)''')
----------------------------

Thanks.
-Chuckk

--http://www.badmuthahubbard.com

Looks like you're dropping an error by redirecting the child process'
standard error into a pipe.

First off, remove the stderr=subprocess.PIPE from
subprocessclient.py. When you do so, you'll start seeing an error
message whenever your code runs:

[jeff@marvin ~]$ python client.py
None
[jeff@marvin ~]$ Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined

That doesn't make a lot of sense at first, until you consider the
following:

[jeff@marvin ~]$ echo 'os.nice(1)' | python
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 'os' is not defined

The Python interpreter is trying to execute the data you write to the
stdin of the child process. Changing the Popen line to use the string
form rather than the sequence form remedies the problem, as does
changing 'string=True' to 'string=False.'

The reason? Because when you set shell=True, additional arguments in
args sequence are passed as additional arguments to the shell itself,
not to the command.
From subprocess.py:

if isinstance(args, types.StringTypes):
args = [args]
else:
args = list(args)

if shell:
args = ["/bin/sh", "-c"] + args

So, in your attempt, you're effectively doing the following:

/bin/sh -c "python2.5" "server.py"

When you want:

/bin/sh -c "python2.5 server.py"

HTH,

Jeff
That helps immensely, Jeff, thank you for explaining all that. I took
out 'shell=True' and it works, and I took out the stdout and stderr
arguments to debug. I am finally able to perform every step in my
plan: start a child process of python running a script that imports
the Csound API; raise the nice level of the parent process; compile
and run a Csound orchestra remotely; and send it notes from the parent
process.
Thanks for your help.
-Chuckk

--
http://www.badmuthahubbard.com
Nov 11 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Do Re Mi chel La Si Do | last post by:
Hi! This script (under Win-XP + P-2.4.1) : import subprocess p1=subprocess.Popen(r'cmd /cdir *.* /S /W /B', stdout=subprocess.PIPE) chaineretour=p1.stdout.read() run OK if called from...
4
by: Marc Carter | last post by:
I am trying to rewrite a PERL automation which started a "monitoring" application on many machines, via RSH, and then multiplexed their collective outputs to stdout. In production there are lots...
6
by: Eric_Dexter | last post by:
I am having trouble contolling vim with subprocess on a windows machine. It appears that vim comes up on the machine all right and it sometimes looks like it is doing the searchs what I am asking...
2
by: Rafael Giannetti Viotti | last post by:
Hi, I am working with the subprocess.py module in Python 2.4.4 and I am confused about it's functionality. It uses the standard pipe-fork-exec method to start a subprocess: # create pipes ...
1
by: WolfgangZ | last post by:
Hello, I'm starting some subprocesses inside a loop. The processes run independent and dont need any communication between each other. Due to memory issues I need to limit the number of running...
2
by: Michael George Lerner | last post by:
Hi, (Python 2.5, OS X 10.4.10) I have a program called pdb2pqr on my system. It is installed so that "pdb2pqr" is in my path and looks like: #\!/bin/zsh -f /sw/share/pdb2pqr/pdb2pqr.py "$@"...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
9
by: erikcw | last post by:
Hi, I have a cgi script where users are uploading large files for processing. I want to launch a subprocess to process the file so the user doesn't have to wait for the page to load. What is...
7
by: Samuel A. Falvo II | last post by:
I have a shell script script.sh that launches a Java process in the background using the &-operator, like so: #!/bin/bash java ... arguments here ... & In my Python code, I want to invoke...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...

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.