Re: Creating a daemon process in Python

2007-02-23 Thread Nick Craig-Wood
Eirikur Hallgrimsson [EMAIL PROTECTED] wrote:
  def daemonize():
  if (not os.fork()):
  # get our own session and fixup std[in,out,err]
  os.setsid()
  sys.stdin.close()
  sys.stdout = NullDevice()
  sys.stderr = NullDevice()

That doesn't close the underlying file descriptors...

Here is another method which does :-

  null = os.open(os.devnull, os.O_RDWR)
  os.dup2(null, sys.stdin.fileno())
  os.dup2(null, sys.stdout.fileno())
  os.dup2(null, sys.stderr.fileno())
  os.close(null)


  if (not os.fork()):
  # hang around till adopted by init
  ppid = os.getppid()
  while (ppid != 1):
  time.sleep(0.5)
  ppid = os.getppid()

Why do you need hang around until adopted by init?  I've never see
that in a daemonize recipe before?

  else:
  # time for child to die
  os._exit(0)
  else:
  # wait for child to die and then bail
  os.wait()
  sys.exit()

-- 
Nick Craig-Wood [EMAIL PROTECTED] -- http://www.craig-wood.com/nick
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-23 Thread Jean-Paul Calderone
On Fri, 23 Feb 2007 08:30:07 -0600, Nick Craig-Wood [EMAIL PROTECTED] wrote:
Eirikur Hallgrimsson [EMAIL PROTECTED] wrote:

 [snip]

  if (not os.fork()):
  # hang around till adopted by init
  ppid = os.getppid()
  while (ppid != 1):
  time.sleep(0.5)
  ppid = os.getppid()

Why do you need hang around until adopted by init?  I've never see
that in a daemonize recipe before?


I think it simplifies some signal handling logic.  I'd never seen it before
in a deamonizer either, but it caught my eye in this one.  I haven't had time
to investigate further though.  I hope Eirikur will explain. :)

Jean-Paul
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-23 Thread Eirikur Hallgrimsson
I didn't actually write this module.  I believe I found it in a 
discussion in ASPN at Active State.

Thanks for the input, and when I get a chance I will try these alternate 
approaches.  This module has been working fine for me as is--so far.

Eirikur


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-22 Thread [EMAIL PROTECTED]
Thanks all,

I understood there is no shortcut function like BSD daemon().  I'll do
it manually using examples from cookbook...



On 2月22日, 午前1:41, Benjamin Niemann [EMAIL PROTECTED] wrote:
 Hello,

 Sakagami Hiroki wrote:
  What is the easiest way to create a daemon process in Python?  Google
  says I should call fork() and other system calls manually, but is
  there no os.daemon() and the like?

 You could try
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

 HTH

 --
 Benjamin Niemann
 Email: pink at odahoda dot de
 WWW:http://pink.odahoda.de/


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-22 Thread Grant Edwards
On 2007-02-22, [EMAIL PROTECTED] [EMAIL PROTECTED] wrote:

 I understood there is no shortcut function like BSD daemon().  I'll do
 it manually using examples from cookbook...

Sure would be nice if somebody posted one. ;)

-- 
Grant Edwards   grante Yow!  Oh, I get it!! The
  at   BEACH goes on, huh,
   visi.comSONNY??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-22 Thread Joshua J. Kugler
Benjamin Niemann wrote:

 What is the easiest way to create a daemon process in Python?  Google
 says I should call fork() and other system calls manually, but is
 there no os.daemon() and the like?
 You could try
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731

Also, more discussion on the topic here:
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012

j

-- 
Joshua Kugler
Lead System Admin -- Senior Programmer
http://www.eeinternet.com
PGP Key: http://pgp.mit.edu/  ID 0xDB26D7CE

-- 
Posted via a free Usenet account from http://www.teranews.com

-- 
http://mail.python.org/mailman/listinfo/python-list

Re: Creating a daemon process in Python

2007-02-21 Thread Benjamin Niemann
Hello,

Sakagami Hiroki wrote:

 What is the easiest way to create a daemon process in Python?  Google
 says I should call fork() and other system calls manually, but is
 there no os.daemon() and the like?

You could try
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/278731


HTH

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread Eirikur Hallgrimsson
Sakagami Hiroki wrote:
 Hi,

 What is the easiest way to create a daemon process in Python?
I find that this works great.  I just pasted my copy, I think you can 
find it via Google.

Eirikur


#  Daemon Module - basic facilities for becoming a daemon process
#  By Coy Krill
#  Combines ideas from Steinar Knutsens daemonize.py and
#  Jeff Kunces demonize.py

Facilities for Creating Python Daemons

import os
import time
import sys

class NullDevice:
def write(self, s):
pass

def daemonize():
if (not os.fork()):
# get our own session and fixup std[in,out,err]
os.setsid()
sys.stdin.close()
sys.stdout = NullDevice()
sys.stderr = NullDevice()
if (not os.fork()):
# hang around till adopted by init
ppid = os.getppid()
while (ppid != 1):
time.sleep(0.5)
ppid = os.getppid()
else:
# time for child to die
os._exit(0)
else:
# wait for child to die and then bail
os.wait()
sys.exit()


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread garrickp
On Feb 21, 9:33 am, Eirikur Hallgrimsson [EMAIL PROTECTED]
wrote:
 Sakagami Hiroki wrote:
  What is the easiest way to create a daemon process in Python?

I've found it even easier to use the built in threading modules:

import time

t1 = time.time()
print t_poc.py called at, t1

import threading

def im_a_thread():
time.sleep(10)
print This is your thread speaking at, time.time()

thread = threading.Thread(target=im_a_thread)
thread.setDaemon(True)
thread.start()
t2 = time.time()
print Time elapsed in main thread:, t2 - t1


Of course, your mileage may vary.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread Benjamin Niemann
[EMAIL PROTECTED] wrote:

 On Feb 21, 9:33 am, Eirikur Hallgrimsson [EMAIL PROTECTED]
 wrote:
 Sakagami Hiroki wrote:
  What is the easiest way to create a daemon process in Python?
 
 I've found it even easier to use the built in threading modules:
 
 import time
 
 t1 = time.time()
 print t_poc.py called at, t1
 
 import threading
 
 def im_a_thread():
 time.sleep(10)
 print This is your thread speaking at, time.time()
 
 thread = threading.Thread(target=im_a_thread)
 thread.setDaemon(True)
 thread.start()
 t2 = time.time()
 print Time elapsed in main thread:, t2 - t1
 
 
 Of course, your mileage may vary.

That's not a daemon process (which are used to execute 'background services'
in UNIX environments).

-- 
Benjamin Niemann
Email: pink at odahoda dot de
WWW: http://pink.odahoda.de/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Creating a daemon process in Python

2007-02-21 Thread garrickp
On Feb 21, 3:34 pm, Benjamin Niemann [EMAIL PROTECTED] wrote:
 That's not a daemon process (which are used to execute 'background services'
 in UNIX environments).

I had not tested this by running the script directly, and in writing a
response, I found out that the entire interpreter closed when the main
thread exited (killing the daemonic thread in the process). This is
different behavior from running the script interactively, and thus my
confusion.

Thanks! ~Garrick

-- 
http://mail.python.org/mailman/listinfo/python-list