Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setDaemon() method of threading.Thread

I am a newbie in python programming, what I understand is that a process can be a daemon, but a thread in a daemon mode, I couldn't understand the usecase of this, I would request the python gurus to help me in understanding this.

like image 251
hue Avatar asked Feb 26 '11 14:02

hue


People also ask

What is thread setDaemon?

The setDaemon() method of the Thread class is used to mark/set a particular thread as either a daemon thread or a user thread. The Java Virtual Machine exits when the only threads running are all daemon threads. This method must be called before the thread is started.

How do you make a daemon thread in Python?

In this example, we have created a function thread_1() and thread T as same as above example but here after the creation of thread T we use setDaemon() method to change the non-daemon nature of thread T to daemon nature, then we start the thread T and temporary stops the execution of the main thread.

What does the thread join () method do in Python?

In python 3. x join() is used to join a thread with the main thread i.e. when join() is used for a particular thread the main thread will stop executing until the execution of joined thread is complete.

What is the difference between daemon and non Daemonic threads?

Daemon threads are low priority threads which always run in background and user threads are high priority threads which always run in foreground. User Thread or Non-Daemon are designed to do specific or complex task where as daemon threads are used to perform supporting tasks.


2 Answers

Here is some basic code using threading:

import Queue import threading  def basic_worker(queue):     while True:         item = queue.get()         # do_work(item)         print(item)         queue.task_done() def basic():     # http://docs.python.org/library/queue.html     queue = Queue.Queue()     for i in range(3):          t = threading.Thread(target=basic_worker,args=(queue,))          t.daemon = True          t.start()     for item in range(4):         queue.put(item)     queue.join()       # block until all tasks are done     print('got here')  basic() 

When you run it, you get

% test.py 0 1 2 3 got here 

Now comment out the line:

         t.daemon = True 

Run it again, and you'll see that the script prints the same result, but hangs. The main thread ends (note that got here was printed), but the second thread never finishes.

In contrast, when t.daemon is set to True, the thread t is terminated when the main thread ends.

Note that "daemon threads" has little to do with daemon processes.

like image 165
unutbu Avatar answered Sep 22 '22 08:09

unutbu


It looks like people intend to use Queue to explain threading, but I think there should be a much simpler way, by using time.sleep(), to demo a daemon thread.

Create daemon thread by setting the daemon parameter (default as None):

from threading import Thread import time  def worker():     time.sleep(3)     print('daemon done')  thread = Thread(target=worker, daemon=True) thread.start()  print('main done') 

Output:

main done  Process finished with exit code 0 

Remove the daemon argument, like:

thread = Thread(target=worker) 

Re-run and see the output:

main done daemon done  Process finished with exit code 0 

Here we already see the difference of a daemon thread:

The entire Python program can exit if only daemon thread is left.


isDaemon() and setDaemon() are old getter/setter API. Using constructor argument, as above, or daemon property is recommended.

like image 26
themefield Avatar answered Sep 20 '22 08:09

themefield