By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,269 Members | 1,561 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,269 IT Pros & Developers. It's quick & easy.

[2.4.2/Linux] Getting Python to fork?

P: n/a
Hello

I need to launch a Python script, and fork it so that the calling
script can resume with the next step will the Python script keeps
running.

I tried those two, but they don't work, as the calling script is stuck
until the Python script ends:

sys.stdout = open(os.devnull, 'w')

=====
#if os.fork():
pid = os.fork()
if pid 0:
sys.exit(0)
=====

Should I use another library to do this?

Thank you.
Feb 4 '08 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Gilles Ganault wrote:
Hello

I need to launch a Python script, and fork it so that the calling
script can resume with the next step will the Python script keeps
running.

I tried those two, but they don't work, as the calling script is stuck
until the Python script ends:

sys.stdout = open(os.devnull, 'w')

=====
#if os.fork():
pid = os.fork()
if pid 0:
sys.exit(0)
=====

Should I use another library to do this?

Thank you.
What OS? Stuck how? I you want both processes to execute, why do you
call sys.exit? I don't think you've shown enough code to tell what you
are doing right or wrong. Try this:

pid = os.fork()
if pid:
# Original fork continues here
# pid is child's process id
# ... so onwards with the next step
# If it needs to wait for the child to complete, it can call os.waitpid
else:
# New fork continues here, independently of the original process
# doing whatever the fork was create for

Gary Herron

Feb 4 '08 #2

P: n/a
On 2008-02-04, Gilles Ganault <no****@nospam.comwrote:
I need to launch a Python script, and fork it so that the calling
script can resume with the next step will the Python script keeps
running.

I tried those two, but they don't work, as the calling script is stuck
until the Python script ends:
This should work I believe:

if os.fork():
os._exit(0)
os.setsid()
os.chdir("/")
fd = os.open("/dev/null", os.O_RDWR)
os.dup2(fd, 0)
os.dup2(fd, 1)
os.dup2(fd, 2)
if fd 2:
os.close(fd)
# do stuff

Although bear in mind it's pretty UNIX-y.
Feb 4 '08 #3

P: n/a
Jon Ribbens wrote:
This should work I believe:

if os.fork():
os._exit(0)
os.setsid()
os.chdir("/")
fd = os.open("/dev/null", os.O_RDWR)
os.dup2(fd, 0)
os.dup2(fd, 1)
os.dup2(fd, 2)
if fd 2:
os.close(fd)
# do stuff

Although bear in mind it's pretty UNIX-y.
IIRC you have to fork a second time after you have changed the working
dir and created a new session group.

Christian

Feb 4 '08 #4

P: n/a
On 2008-02-04, Christian Heimes <li***@cheimes.dewrote:
>Although bear in mind it's pretty UNIX-y.

IIRC you have to fork a second time after you have changed the working
dir and created a new session group.
Why? I don't think you do.
Neither does BSD daemon.c or glibc daemon.c
Feb 4 '08 #5

P: n/a
On 2008-02-04, Bernard <be***********@gmail.comwrote:
#Fork and commit suicide
if os.fork():
sys.exit(0)
I'm pretty sure that should be os._exit(0)
#What to do in parent process
This is now the child process.
sys.stdin = open('/dev/null')
sys.stdout = open('/dev/null', 'w')
sys.stderr = open('/dev/null', 'w')
I think that's changing Python's idea of stdin etc but not the
operating system's idea of them. You won't be closing the original
file descriptors, and if you run any subprocesses they will end up
with the original stdin/out/err. Unless sys.stdin is more magic
than I'm aware of.
Feb 4 '08 #6

P: n/a
Jon Ribbens wrote:
I think that's changing Python's idea of stdin etc but not the
operating system's idea of them. You won't be closing the original
file descriptors, and if you run any subprocesses they will end up
with the original stdin/out/err. Unless sys.stdin is more magic
than I'm aware of.
Jon is correct here. You must close or redirect the underlying C file
descriptor. Python's sys.std streams don't magically do this for you
because Python keeps a backup of the standard streams for internal
purpose in sys.__std*__. os.dup2 is the best solution.

Christian

Feb 4 '08 #7

P: n/a
To create a deamon, you indeed need to fork two times. For more
information and a working example see:
http://aspn.activestate.com/ASPN/Coo.../Recipe/278731 . I'm
quite sure this works, because I used it several times to create a deamon.
Jon Ribbens wrote:
On 2008-02-04, Christian Heimes <li***@cheimes.dewrote:
>>Although bear in mind it's pretty UNIX-y.
IIRC you have to fork a second time after you have changed the working
dir and created a new session group.

Why? I don't think you do.
Neither does BSD daemon.c or glibc daemon.c
Feb 4 '08 #8

P: n/a
On 2008-02-04, Rolf van de Krol <py****@rolfvandekrol.nlwrote:
To create a deamon, you indeed need to fork two times. For more
information and a working example see:
http://aspn.activestate.com/ASPN/Coo.../Recipe/278731 . I'm
quite sure this works, because I used it several times to create a deamon.
That doesn't mean it works. That just means it hasn't failed while
you were watching.

(Not that I am saying it's necessarily wrong, I'm just saying that
"it worked when I tried it" is a very bad way of deciding if something
is correct code.)
Feb 4 '08 #9

P: n/a
On Mon, 04 Feb 2008 16:40:01 +0100, Rolf van de Krol
<py****@rolfvandekrol.nlwrote:
>To create a deamon, you indeed need to fork two times.
Do I really need this much complication just to exit the script and
let a child handle the pop-up?

I've changed this line, and the parent still doesn't return, and the
script waits until the child ends before resuming to the next step:

if os.fork():
#BAD? sys.exit(0)
os._exit(0)
else:

Thanks.
Feb 13 '08 #10

This discussion thread is closed

Replies have been disabled for this discussion.