setDaemon problem.
Diez B. Roggisch
deets at nospam.web.de
Fri Apr 20 06:49:38 EDT 2007
Ramashish Baranwal schrieb:
> 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.
Diez
More information about the Python-list
mailing list