syslogd (sorta)

Jordan Krushen jordan at krushen.com
Wed Apr 30 23:54:38 EDT 2003


On Thu, 01 May 2003 03:03:59 GMT, Cecil H. Whitley <cwhitley at earthlink.net> 
wrote:

> I am trying to write a syslogd server in python.  How can I "end" the 
> main thread and have a daemonic thread continue?

You can't.  From the threading library docs:

The entire Python program exits when no active non-daemon threads are left.

Thus, if you have a main, non-daemon thread, and any number of daemonized 
ones, as soon as the main thread ceases activity, they all die.

> thread = threading.Thread(target=syslogd, name=syslogd, args=[], 
> kwargs={})
> thread.isDaemon(True)
> thread.start()

If you want to daemonize the thread, use thread.setDaemon(), not isDaemon() 
.  See below, however.

> Everything works just fine, up until I hit return of course.  So where
> should I be looking in order to write a "clean" background process?

Assuming that your isDaemon() line there is a typo (you'll get an error 
with the above) and that you're actually calling setDaemon(), just comment 
out that line.  The background thread will be non-daemonized, and will 
consequently keep the program alive.

As an example, the following will run until the waiting() function finishes 
(daemonizing it only lets it print "waiting" the first time, as the main 
thread finishes right after start(), terminating the program:

import threading, time

def waiting():
    for i in range(3):
        print "waiting"
        time.sleep(1)

t = threading.Thread(target=waiting)
t.start()

HTH,

J.




More information about the Python-list mailing list