Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Subprocess and pipe-fork-exec primitive

Reply
Thread Tools

Subprocess and pipe-fork-exec primitive

 
 
Rafael Giannetti Viotti
Guest
Posts: n/a
 
      07-30-2007
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

pid = fork()

if pid == 0:
# child
exec(...)

# parent
status = waitpid(pid, 0)

From my experience, this primitive will fail with 'no child
processes' at the waitpid call if the forked child dies very quickly -
before the parent is scheduled back for execution. This seems to happen
because Python has a default SIGCHLD handler that, in this case, will
reap the process before the parent has the chance to do it.

I would like to know if this is correct, or am I missing something here?

---
Rafael.

 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=
Guest
Posts: n/a
 
      07-31-2007
> From my experience, this primitive will fail with 'no child processes'
> at the waitpid call if the forked child dies very quickly - before the
> parent is scheduled back for execution. This seems to happen because
> Python has a default SIGCHLD handler that, in this case, will reap the
> process before the parent has the chance to do it.


What operating system is your experience from? On a POSIX system,
this should not happen - i.e. delivery of SIGCHLD should not cause
to make the child waited-for. Python itself does not perform wait()
in response to SIGCHLD.

> I would like to know if this is correct, or am I missing something here?


You must be missing something, although I'm uncertain what precisely
that is.

Regards,
Martin
 
Reply With Quote
 
 
 
 
Rafael V.
Guest
Posts: n/a
 
      08-01-2007
Hi Martin,

the operating system I'm using is SUSE Linux 10, kernel 2.6.13.

You're right, I was missing something. After you told me that it
couldn't be Python preforming wait() on SIGCHLD, I decided to
investigate further.

My application requires access to a Informix database, and uses
informixdb. The problem seems to be related to that module. I wrote a
small piece of code to test it:

---------------------------------------------------
#!/usr/bin/env python

from os import fork, execl, waitpid
from informixdb import connect

try:
conf = {}
conf['dsn'] = 'db@server'
conf['user'] = 'user'
conf['password'] = 'password'
connection = connect(**conf)

except:
pass

pid = fork()

if pid == 0:
# Child
execl("/bin/sh", "/bin/sh", "-c", "true")

# Parent
waitpid(pid, 0)

print pid
---------------------------------------------------

If you run the code above multiple times, some runs will trigger
exceptions on the waitpid calls. Commenting the call to
informixdb.connect(), no exceptions are triggered. I am concluding that
informixdb, not Python, is handling the signals and reaping the
subprocesses.

Now Martin, do you think I can use informixdb.py and subprocess.py in
the same application? I was thinking on forking subprocesses from the
main thread and using other threads to access the Informix database,
would that work?

Thanks,
Rafael.

Martin v. Löwis escreveu:
>> From my experience, this primitive will fail with 'no child processes'
>> at the waitpid call if the forked child dies very quickly - before the
>> parent is scheduled back for execution. This seems to happen because
>> Python has a default SIGCHLD handler that, in this case, will reap the
>> process before the parent has the chance to do it.

>
> What operating system is your experience from? On a POSIX system,
> this should not happen - i.e. delivery of SIGCHLD should not cause
> to make the child waited-for. Python itself does not perform wait()
> in response to SIGCHLD.
>
>> I would like to know if this is correct, or am I missing something here?

>
> You must be missing something, although I'm uncertain what precisely
> that is.
>
> Regards,
> Martin




 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Primitive Integer and Built-In Operator Type Coecion and Result Andrew Tomazos C++ 0 11-07-2011 11:37 PM
how to import subprocess into my 'subprocess.py' file hiral Python 2 05-05-2010 12:56 PM
Default primitive values from primitive Class<?> object. Daniel Pitts Java 7 10-23-2008 04:30 PM
[Subprocess/Windows] subprocess module under Windows 98 Andreas Jung Python 2 11-02-2005 05:41 PM
Primitive vs. non-primitive l-value richardclay09@yahoo.co.uk C++ 7 05-09-2005 02:52 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57