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

subprocess confusion

Hi,
Just discovered that my subprocess call with the preexec_fn wasn't doing
what I thought.
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.

Should I just put the remsh in the actual command instead of preexec_fn?
thanks,
--Tim Arnold
-------------------------------
if machine == socket.gethostname():
shname = None
else:
shname = lambda :'/bin/remsh %s ' % (machine)
p = subprocess.Popen(preexec_fn = shname,
shell = True,
args = command,
stderr = subprocess.STDOUT,
stdout = log,
env = env,
)
try:
p.wait()
if log:
log.close()
except:
pass

-------------------------------
Apr 16 '07 #1
3 1650
Tim Arnold wrote:
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.
I don't think the return value of preexec_fn is used for anything. You
can use to do things like set process group IDs in the child. For your
use, everything needs to be args.

If you are using Python 2.5, and log is a file, you want:

from __future__ import with_statement

with log:
if machine != socket.gethostname():
command = "/bin/remsh %s %s" % (machine, command)

subprocess.check_call(command, shell=True, env=env,
stderr=subprocess.STDOUT, stdout=log)

The semantics are slightly different, since log will always close this
way, while in the other example, you have left it open if there is an
exception.
--
Michael Hoffman
Apr 16 '07 #2
Tim Arnold <ti****@sas.comwrote:
Just discovered that my subprocess call with the preexec_fn wasn't doing
what I thought.
If 'machine' value is different than the current machine name, I want to
remsh the command to that machine, but obviously I misunderstood the
preexec_fn arg.

Should I just put the remsh in the actual command instead of
preexec_fn?
Yes.

The preexec_fn is run after the fork() but before the exec(). Ie a
new process has been made, but it hasn't started your task yet.

For example a classic use of preexec_fn is

preexec_fn=os.setsid

You seem to be thinking it is pre-pending something to your command
line which isn't how it works.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Apr 17 '07 #3
"Nick Craig-Wood" <ni**@craig-wood.comwrote in message
news:sl*****************@irishsea.home.craig-wood.com...
Tim Arnold <ti****@sas.comwrote:
<snip>
> Should I just put the remsh in the actual command instead of
preexec_fn?

Yes.

The preexec_fn is run after the fork() but before the exec(). Ie a
new process has been made, but it hasn't started your task yet.

For example a classic use of preexec_fn is

preexec_fn=os.setsid

You seem to be thinking it is pre-pending something to your command
line which isn't how it works.

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Thanks much to you and Michael H. for the great explanations.
Now everything is working fine, and I understand subprocess a little better!

--Tim Arnold
Apr 17 '07 #4

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

Similar topics

2
by: Stewart Midwinter | last post by:
this has me puzzled; I've created a small test app to show the problem I'm having. I want to use subprocess to execute system commands from inside a Tkinter app running under Cygwin. When I...
3
by: Darren Dale | last post by:
I'm a developer on the matplotlib project, and I am having trouble with the subprocess module on windows (Python 2.4.2 on winXP). No trouble to report with linux. I need to use _subprocess instead...
0
by: Mitja Trampus | last post by:
I am trying to run a subprocess within given time and memory restrictions. The resource module kind of works for me, but I do not understand why and am seeking an explanation. Also, the signal...
5
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend...
9
by: Clodoaldo Pinto Neto | last post by:
Output from the shell: $ set | grep IFS IFS=$' \t\n' Output from subprocess.Popen(): "IFS=' \t\n" Both outputs for comparison:
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
9
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This...
10
by: JD | last post by:
Hi, I want send my jobs over a whole bunch of machines (using ssh). The jobs will need to be run in the following pattern: (Machine A) (Machine B) (Machine C) Job A1 Job B1 ...
12
by: bhunter | last post by:
Hi, I've used subprocess with 2.4 several times to execute a process, wait for it to finish, and then look at its output. Now I want to spawn the process separately, later check to see if it's...
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: 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: 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...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.