GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing thepythoncode.com — Daemon Threads in Python - The Python Code Turn your code into any language with our Code Converter. It's the ultimate tool for multi-language programming. Start converting now! In this tutorial, you will learn what are daemon threads in Python and how to set them up, you should have a basic knowledge about threads to follow up in this tutorial. A daemon thread is a thread that dies whenever the main thread dies, it is also called a non-blocking thread. Usually, the main thread should wait for other threads to finish in order to quit the program, but if you set the daemon flag, you can let the thread do its work and forget about it, and when the program quits, it will be killed automatically. This is useful for many scenarios, suppose you are doing some web scraping tasks, and you want to make a thread that alerts you in email whenever a new item is inserted in that particular website you're scraping. Another example you may want to make a thread that watches for log files in your program, and alert you when a critical error is occured. In the end of the tutorial, we'll share with you some tutorials that we found useful to use daemon threads in. Now that you know what a daemon thread is, and you know it's useful, let's take a demonstration example, the following code initiates a non-daemon thread and start it, and then we end the main thread as well:
The However, if you run it, you'll see the program keeps running and won't allow you to exit (only if you close the window):
So the non-daemon thread is making the program refuses to exit until it's finished. The following example initiates a daemon thread this time:
Now we pass Let's execute the program:
This time once the main thread finishes its work, the daemon thread also gets killed automatically and exits the program. Real ExamplesBelow are some of the tutorials in which we used daemon threads to accomplish our tasks:
Happy coding ♥ Just finished the article? Now, boost your next project with our Python Code Generator. Discover a faster, smarter way to code.
Sharing is caring!
Read alsoHow to Use Threads for IO Tasks in PythonUsing different methods such as thread pool executor or threading module to create and handle threads for speeding up I/O bound tasks in Python. Logging in PythonLearn how to use Python's built-in logging module to log your Python applications, changing default format, level, and learning in a concrete example, as well as using logging handlers.
How to Build a WiFi Scanner in Python using ScapyBuilding a Wi-Fi scanner in Python using Scapy that finds and displays available nearby wireless networks and their MAC address, dBm signal, channel and encryption type. CommentsGot a coding query or need some guidance before you comment? Check out this Python Code Assistant for expert advice and handy tips. It's like having a coding tutor right in your fingertips! |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |