{"id":301,"date":"2024-01-28T10:21:00","date_gmt":"2024-01-28T10:21:00","guid":{"rendered":"https:\/\/learnpython.elegantwallp.com\/?p=301"},"modified":"2024-01-28T10:21:01","modified_gmt":"2024-01-28T10:21:01","slug":"python-daemon-threads","status":"publish","type":"post","link":"https:\/\/learnpython.elegantwallp.com\/2024\/01\/28\/python-daemon-threads\/","title":{"rendered":"Python Daemon\u00a0Threads"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you\u2019ll learn about Python daemon threads and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Introduction to the Python daemon&nbsp;threads<\/h2>\n\n\n\n<p>In Python, every program has at least one thread called the main thread. To create a program that has more than one thread, you use the\u00a0threading\u00a0module. By using multiple threads, you can execute tasks concurrently.<\/p>\n\n\n\n<p>Sometimes, you may want to execute a task in the background. To do that you use a special kind of thread called a&nbsp;<strong>daemon thread<\/strong>.<\/p>\n\n\n\n<p>By definition, daemon threads are&nbsp;<strong>background threads<\/strong>. In other words, daemon threads execute tasks in the background.<\/p>\n\n\n\n<p>Daemon threads are helpful for executing tasks that support non-daemon threads in the program. For example:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Log information to a file in the background.<\/li>\n\n\n\n<li>Scrap contents from a website in the background.<\/li>\n\n\n\n<li>Auto-save the data into a database in the background.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a daemon thread<\/h2>\n\n\n\n<p>To create a daemon thread, you set the\u00a0<code>daemon<\/code>\u00a0to\u00a0<code>True<\/code>\u00a0in the\u00a0<code>Thread<\/code>\u00a0constructor:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>t = Thread(target=f, deamon=True)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Alternatively, you can set the\u00a0<code>daemon<\/code>\u00a0property to\u00a0<code>True<\/code>\u00a0after creating the\u00a0<code>Thread<\/code>\u2018s instance:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>t = Thread(target=f) t.deamon = True<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">A daemon thread example<\/h2>\n\n\n\n<p>The following example shows how to create a non-daemon thread that shows the number of seconds that the program has been waiting for:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from threading import Thread import time def show_timer(): count = 0 while True: count += 1 time.sleep(1) print(f'Has been waiting for {count} second(s)...') t = Thread(target=show_timer) t.start() answer = input('Do you want to exit?\\n')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a function&nbsp;<code>show_timer()<\/code>&nbsp;that displays the number of seconds that the program has been waiting for.<\/p>\n\n\n\n<p>Second, create a new thread that executes the\u00a0<code>show_timer()<\/code>\u00a0function:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>t = Thread(target=show_timer)<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Third, start the thread:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>t.start()<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>Finally, call the\u00a0<code>input()<\/code>\u00a0function to prompt users for input:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>answer = input('Do you want to exit?\\n')<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you run the program, it\u2019ll show the following output and run forever.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Do you want to exit?Has been waiting for 1 second(s)... Has been waiting for 2 second(s)... Has been waiting for 3 second(s)... Has been waiting for 4 second(s)... Y Has been waiting for 5 second(s)... Has been waiting for 6 second(s)...<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>To terminate the program, you need to kill the terminal.<\/p>\n\n\n\n<p>The program runs indefinitely because the thread&nbsp;<code>t<\/code>&nbsp;is a non-daemon thread. The program needs to wait for all non-daemon threads to complete before it can exit.<\/p>\n\n\n\n<p>Now, let\u2019s turn the thread into a daemon thread:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>from threading import Thread import time def show_timer(): count = 0 while True: count += 1 time.sleep(1) print(f'Has been waiting for {count} second(s)...') t = Thread(target=show_timer, daemon=True) t.start() answer = input('Do you want to exit?\\n') <\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>If you run the program, input something, and hit enter, the program will terminate. For example:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><code>Do you want to exit? Has been waiting for 1 second(s)... Y<\/code><small>Code language: Python (python)<\/small><\/code><\/pre>\n\n\n\n<p>The program terminates because it doesn\u2019t need to wait for the daemon thread to complete. Also, the daemon thread is killed automatically when the program exits.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Daemon threads vs. non-daemon threads<\/h2>\n\n\n\n<p>The following table illustrates the differences between daemon and non-daemon threads:<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table><thead><tr><th><\/th><th>Daemon Threads<\/th><th>Non-daemon Threads<\/th><\/tr><\/thead><tbody><tr><td>Thread creation<\/td><td><code>t = Thread(target=f, daemon=True)<\/code><\/td><td><code>t = Thread(target=f)<\/code><\/td><\/tr><tr><td>The program needs to wait before exiting<\/td><td>No<\/td><td>Yes<\/td><\/tr><tr><td>Kind of tasks<\/td><td>Not critical like logging<\/td><td>Critical<\/td><\/tr><\/tbody><\/table><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you\u2019ll learn about Python daemon threads and how to use them effectively. Introduction to the Python daemon&nbsp;threads In Python, every program has at least one thread called the main thread. To create a program that has more than one thread, you use the\u00a0threading\u00a0module. By using multiple threads, you can execute tasks [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[41],"tags":[],"class_list":["post-301","post","type-post","status-publish","format-standard","hentry","category-1-python-concurrency"],"_links":{"self":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/301","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/comments?post=301"}],"version-history":[{"count":1,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/301\/revisions"}],"predecessor-version":[{"id":302,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/posts\/301\/revisions\/302"}],"wp:attachment":[{"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/media?parent=301"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/categories?post=301"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learnpython.elegantwallp.com\/wp-json\/wp\/v2\/tags?post=301"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}