Change Main Thread To Daemon Thread
If we want to change the main thread which is always non-daemon in nature to daemon nature then we will get a RuntimeError because when the program is started at a time main thread is also started so the main thread is an active thread and the active thread is not set to the daemon.
Example:
Python3
# import module from threading import * print (current_thread().setDaemon( True )) |
Output:
RuntimeError: cannot set daemon status of active thread
Python Daemon Threads
The threads which are always going to run in the background that provides supports to main or non-daemon threads, those background executing threads are considered as Daemon Threads. The Daemon Thread does not block the main thread from exiting and continues to run in the background. This article is based on threading in python, here we discuss daemon thread with examples.
There is one of the best examples of a daemon thread is Garbage Collector because we assume that the main thread is executing or running, at that time any memory problem occurs then immediately python virtual machine(PVM) is going to execute Garbage Collector. The Garbage Collector is going to execute in the background and destroy all the useless objects and then free memory by default will be provided, once there is free memory will available then the main thread is going to be executed without any problem.