setDaemon problem.
Ramashish Baranwal
ramashish.lists at gmail.com
Fri Apr 20 09:43:28 EDT 2007
> > Hi,
>
> > I am facing an issue in daemonizing a thread using setDaemon method.
> > Here is my code-
>
> > import time
> > from threading import Thread
>
> > class MThread(Thread):
> > def run(self):
> > f = open('/tmp/t.log', 'w')
> > for i in range(10):
> > f.write('Iteration %d\n' % i)
> > time.sleep(1)
> > f.close()
>
> > if __name__ == "__main__":
> > t = MThread()
> > t.setDaemon(True)
> > print 'Starting thread'
> > t.start()
>
> > The scripts runs all fine, but nothing gets logged to "/tmp/t.log".
> > However when I run the same script without setting thread as daemon
> > (no call to t.setDaemon), everything works fine.
>
> > Am I missing anything?
>
> Yes. You miss the documentation of setDaemon. It is _not_ a
> PROCESS-daemonization recipe - which you can get here:
>
> http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/66012
>
> Instead, it means that the thread started is considered not to be worth
> waiting for when the main thread exits. Which it does immediately in
> your code, resulting in nothing written in the logfile.
>
> I think the name setDaemon is somewhat unfortunate.
>
Thanks Diez,
I was also wondering about "daemonizing" a thread, but I interpreted
that it would daemonize the process which it didn't. I think setDaemon
should be renamed to setDetached or something similar.
Ram
More information about the Python-list
mailing list