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

[2.4.2/Linux] Getting Python to fork?

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
9 5246
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
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
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Mike Dee | last post by:
A very very basic UTF-8 question that's driving me nuts: If I have this in the beginning of my Python script in Linux: #!/usr/bin/env python # -*- coding: UTF-8 -*- should I - or should I...
1
by: elastic | last post by:
I'm trying to implement a simple timer to work on win32 (98,2k,xp), solaris and linux. where the last two are simple, the first seems to be problematic. the problem is that the kill command seems...
26
by: blah | last post by:
ok, i m going to use Linux for my Python Programs, mainly because i need to see what will these fork() and exec() do. So, can anyone tell me which flavour of linux i should use, some say that...
6
by: cmk128 | last post by:
Hi here is my c file, compile in gcc 3.X in linux: #include <stdio.h> int main() { printf("Hello\n"); if (fork() == 0) printf("world! \n"); }
14
by: Rochester | last post by:
Hi, I just found out that the general open file mechanism doesn't work for named pipes (fifo). Say I wrote something like this and it simply hangs python: #!/usr/bin/python import os
4
by: Paolo Pantaleo | last post by:
Hi, I need to capture a screen snapshot in Linux. PIL has a module IageGrab, but in the free version it only works under Windows. Is there any package to capture the screen on Linux? Thnx...
0
by: Kurt B. Kaiser | last post by:
Patch / Bug Summary ___________________ Patches : 423 open ( +2) / 3553 closed ( +4) / 3976 total ( +6) Bugs : 963 open (+20) / 6479 closed ( +8) / 7442 total (+28) RFE : 260 open...
8
by: Ulysse | last post by:
Hello, I have a python script which runs all the time (using of library threading). I would like this scipt to run on a remote linux Os using Putty. The problem is, when I close Putty command...
2
by: ganeshp | last post by:
Hi , Below given is my code: This generates the below given error. Can any one help me out on this error please ? Actually i have a huge project written using C++ language on linux , I want...
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
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: 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...
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...

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.