473,769 Members | 2,345 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

[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 5265
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****@rolfvan dekrol.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****@rolfvan dekrol.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
8229
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 not - be able to use non-ASCII characters in strings and in Tk GUI button labels and GUI window titles and in raw_input data without Python returning wrong case in manipulated
1
4337
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 to always fail. the pid is always negative and doesn't apear in the task manager. this is the perl version I'm using : This is perl, v5.6.1 built for MSWin32-x86-multi-thread (with 1 registered patch, see perl -V for more detail)
26
2606
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 Debian is more programmer friendly, or shold i use fedora, or Solaris. Because these three are the only ones i know of that are popular and free. -- * Posted with NewsLeecher v3.0 Beta 7 * http://www.newsleecher.com/?usenet
6
3321
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
19800
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
4005
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 PAolo
0
275
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 ( +0) / 250 closed ( +0) / 510 total ( +0) New / Reopened Patches ______________________
8
8818
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 line window running on my Win PC, the python script stops to run too. I tried to use cron tables instead. By setting the time and restart cron process, but it's not practical.
2
6723
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 to make that project work on windows, so i tried this small program on windows to check and compiled using gcc ,and i got the below given error. 1) This project on linux uses APIs like pipe, waitpid, usleep,fork,wait and constants like WHOHANG
0
9589
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
9999
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8876
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3967
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3570
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.