0

I am very confused about daemon and non daemon thread in python. I read that non daemon thread exits when main thread exit in Python!

But here I read that in Java daemon thread runs in the background!

I have read so many different discussions on stackoverflow about daemon and non daemon threads, but still I am confused!

Can you please clarify which thread is able to run in the background using Python?

Community
  • 1
  • 1
maq
  • 1,175
  • 3
  • 17
  • 34

2 Answers2

1

I think you may be confusing threads and processes. Both threads and processes offer concurrent execution sequences, one thing "running in the background" while another executes, as you put it. One main difference between them is threads have shared memory while processes do not.

The question you reference refers to UNIX daemon processes, which are usually background processes like sshd or something. These are a bit different from daemon threads.

In Python and java similarly a daemon thread is a thread that will not block the entire program from exiting. When all the non-daemon threads are finished running the daemon threads will just be stopped (maybe abruptly).

TL;DR Both daemon and non daemon threads will execute independently and concurrently (sort of, make sure you understand https://wiki.python.org/moin/GlobalInterpreterLock) in Python, not sure what your use case is, but I think non-daemon threads are more common and are probably what you want.

qwwqwwq
  • 6,999
  • 2
  • 26
  • 49
  • consider i have a non-daemon thread which is doing a heavy operation so i forcely stop my program so non-daemon thread will also terminate?? – maq Jun 14 '15 at 06:35
  • If you terminate a process, everything in that process is terminated, including any Python thread, dæmon or not. – Ulrich Eckhardt Jun 14 '15 at 07:54
1

Try this:

import threading
help(threading.Thread)

then scroll down to the documentation of the data descriptors, in particular the "daemon" flag. There, you will find this info:

daemon
   A boolean value indicating whether this thread is a daemon thread.

   This must be set before start() is called, otherwise RuntimeError is
   raised. Its initial value is inherited from the creating thread; the
   main thread is not a daemon thread and therefore all threads created in
   the main thread default to daemon = False.

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

In particular the last sentence is important, because all your questions are implicitly answered by that. Note also that this contradicts your first claim that "non daemon thread exits when main thread exit". Also, other languages behave differently, for example your reference to Java may be true, but useless to explain Python behaviour. Checking the documentation, it seems that Java uses the term "daemon thread" exactly as Python does.

Ulrich Eckhardt
  • 16,572
  • 3
  • 28
  • 55
  • :/ consider i have a non-daemon thread in python which is doing a heavy operation so i forcely stop my program so non-daemon thread will also terminate?? – maq Jun 14 '15 at 07:51
  • 1
    If you terminate a process, everything in that process is terminated, including any Python thread, dæmon or not. Terminating a program is an OS-level operation, the internal operations of Python running inside that process are of course affected by that, too. – Ulrich Eckhardt Jun 14 '15 at 07:56
  • thankssss @Ulrich you now clear my all concept u shld add it in ur answer so i will accept it :) – maq Jun 14 '15 at 08:31
  • thank you now i understand please include in ur answer so i can accept it :) – maq Jun 14 '15 at 10:58