{"id":4073,"date":"2022-07-13T09:16:30","date_gmt":"2022-07-13T09:16:30","guid":{"rendered":"https:\/\/www.pythontutorial.net\/?page_id=4073"},"modified":"2023-01-13T00:38:45","modified_gmt":"2023-01-13T00:38:45","slug":"python-daemon-threads","status":"publish","type":"page","link":"https:\/\/www.pythontutorial.net\/python-concurrency\/python-daemon-threads\/","title":{"rendered":"Python Daemon\u00a0Threads"},"content":{"rendered":"\n<p><strong>Summary<\/strong>: in this tutorial, you&#8217;ll learn about Python daemon threads and how to use them effectively.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id='introduction-to-the-python-daemonthreads'>Introduction to the Python daemon&nbsp;threads <a href=\"#introduction-to-the-python-daemonthreads\" class=\"anchor\" id=\"introduction-to-the-python-daemonthreads\" title=\"Anchor for Introduction to the Python daemon&nbsp;threads\">#<\/a><\/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 <a href=\"https:\/\/www.pythontutorial.net\/advanced-python\/python-multithreading-example\/\">threading<\/a> module. 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 <strong>daemon thread<\/strong>.<\/p>\n\n\n\n<p>By definition, daemon threads are <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\" id='creating-a-daemon-thread'>Creating a daemon thread <a href=\"#creating-a-daemon-thread\" class=\"anchor\" id=\"creating-a-daemon-thread\" title=\"Anchor for Creating a daemon thread\">#<\/a><\/h2>\n\n\n\n<p>To create a daemon thread, you set the <code>daemon<\/code> to <code>True<\/code> in the <code>Thread<\/code> constructor:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">t = Thread(target=f, deamon=<span class=\"hljs-literal\">True<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Alternatively, you can set the <code>daemon<\/code> property to <code>True<\/code> after creating the <code>Thread<\/code>&#8216;s instance:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">t = Thread(target=f)\nt.deamon = <span class=\"hljs-literal\">True<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h2 class=\"wp-block-heading\" id='a-daemon-thread-example'>A daemon thread example <a href=\"#a-daemon-thread-example\" class=\"anchor\" id=\"a-daemon-thread-example\" title=\"Anchor for A daemon thread example\">#<\/a><\/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<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\"><span class=\"hljs-keyword\">from<\/span> threading <span class=\"hljs-keyword\">import<\/span> Thread\n<span class=\"hljs-keyword\">import<\/span> time\n\n\n<span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_timer<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n    count = <span class=\"hljs-number\">0<\/span>\n    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n        count += <span class=\"hljs-number\">1<\/span>\n        time.sleep(<span class=\"hljs-number\">1<\/span>)\n        print(<span class=\"hljs-string\">f'Has been waiting for <span class=\"hljs-subst\">{count}<\/span> second(s)...'<\/span>)\n\n\nt = Thread(target=show_timer)\nt.start()\n\nanswer = input(<span class=\"hljs-string\">'Do you want to exit?\\n'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>How it works.<\/p>\n\n\n\n<p>First, define a function <code>show_timer()<\/code> 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 <code>show_timer()<\/code> function:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">t = Thread(target=show_timer)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Third, start the thread:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">t.start()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, call the <code>input()<\/code> function to prompt users for input:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">answer = input(<span class=\"hljs-string\">'Do you want to exit?\\n'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you run the program, it&#8217;ll show the following output and run forever.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Do you want to exit?Has been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">1<\/span> second(s)...\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">2<\/span> second(s)...\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">3<\/span> second(s)...\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">4<\/span> second(s)...\nY\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">5<\/span> second(s)...\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">6<\/span> second(s)...<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\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 <code>t<\/code> 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&#8217;s turn the thread into a daemon thread:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python shcb-code-table\"><span class='shcb-loc'><span><span class=\"hljs-keyword\">from<\/span> threading <span class=\"hljs-keyword\">import<\/span> Thread\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-keyword\">import<\/span> time\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span><span class=\"hljs-function\"><span class=\"hljs-keyword\">def<\/span> <span class=\"hljs-title\">show_timer<\/span><span class=\"hljs-params\">()<\/span>:<\/span>\n<\/span><\/span><span class='shcb-loc'><span>    count = <span class=\"hljs-number\">0<\/span>\n<\/span><\/span><span class='shcb-loc'><span>    <span class=\"hljs-keyword\">while<\/span> <span class=\"hljs-literal\">True<\/span>:\n<\/span><\/span><span class='shcb-loc'><span>        count += <span class=\"hljs-number\">1<\/span>\n<\/span><\/span><span class='shcb-loc'><span>        time.sleep(<span class=\"hljs-number\">1<\/span>)\n<\/span><\/span><span class='shcb-loc'><span>        print(<span class=\"hljs-string\">f'Has been waiting for <span class=\"hljs-subst\">{count}<\/span> second(s)...'<\/span>)\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><mark class='shcb-loc'><span>t = Thread(target=show_timer, daemon=<span class=\"hljs-literal\">True<\/span>)\n<\/span><\/mark><span class='shcb-loc'><span>t.start()\n<\/span><\/span><span class='shcb-loc'><span>\n<\/span><\/span><span class='shcb-loc'><span>answer = input(<span class=\"hljs-string\">'Do you want to exit?\\n'<\/span>)\n<\/span><\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>If you run the program, input something, and hit enter, the program will terminate. For example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"Python\" data-shcb-language-slug=\"python\"><span><code class=\"hljs language-python\">Do you want to exit?\nHas been waiting <span class=\"hljs-keyword\">for<\/span> <span class=\"hljs-number\">1<\/span> second(s)...\nY<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">Python<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">python<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The program terminates because it doesn&#8217;t 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\" id='daemon-threads-vs-non-daemon-threads'>Daemon threads vs. non-daemon threads <a href=\"#daemon-threads-vs-non-daemon-threads\" class=\"anchor\" id=\"daemon-threads-vs-non-daemon-threads\" title=\"Anchor for Daemon threads vs. non-daemon threads\">#<\/a><\/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\n\n\n<h2 class=\"wp-block-heading\" id='summary'>Summary <a href=\"#summary\" class=\"anchor\" id=\"summary\" title=\"Anchor for Summary\">#<\/a><\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li>A daemon thread is a background thread. <\/li>\n\n\n\n<li>A daemon thread is useful for executing tasks that are not critical.<\/li>\n\n\n\n<li>The program can exit and doesn&#8217;t need to wait for the daemon threads to be completed.<\/li>\n\n\n\n<li>A daemon thread is automatically killed when the program exits.<\/li>\n<\/ul>\n<div class=\"helpful-block-content\" data-title=\"\">\n\t<header>\n\t\t<div class=\"wth-question\">Was this tutorial helpful ?<\/div>\n\t\t<div class=\"wth-thumbs\">\n\t\t\t<button\n\t\t\t\tdata-post=\"4073\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-daemon-threads\/\"\n\t\t\t\tdata-post-title=\"Python Daemon\u00a0Threads\"\n\t\t\t\tdata-response=\"1\"\n\t\t\t\tclass=\"wth-btn-rounded wth-yes-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t\tclass=\"feather feather-thumbs-up block w-full h-full\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M14 9V5a3 3 0 0 0-3-3l-4 9v11h11.28a2 2 0 0 0 2-1.7l1.38-9a2 2 0 0 0-2-2.3zM7 22H4a2 2 0 0 1-2-2v-7a2 2 0 0 1 2-2h3\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> Yes <\/span>\n\t\t\t<\/button>\n\n\t\t\t<button\n\t\t\t\tdata-response=\"0\"\n\t\t\t\tdata-post=\"4073\"\n\t\t\t\tdata-post-url=\"https:\/\/www.pythontutorial.net\/python-concurrency\/python-daemon-threads\/\"\n\t\t\t\tdata-post-title=\"Python Daemon\u00a0Threads\"\n\t\t\t\tclass=\"wth-btn-rounded wth-no-btn\"\n\t\t\t>\n\t\t\t\t<svg\n\t\t\t\t\txmlns=\"http:\/\/www.w3.org\/2000\/svg\"\n\t\t\t\t\tviewBox=\"0 0 24 24\"\n\t\t\t\t\tfill=\"none\"\n\t\t\t\t\tstroke=\"currentColor\"\n\t\t\t\t\tstroke-width=\"2\"\n\t\t\t\t\tstroke-linecap=\"round\"\n\t\t\t\t\tstroke-linejoin=\"round\"\n\t\t\t\t>\n\t\t\t\t\t<path\n\t\t\t\t\t\td=\"M10 15v4a3 3 0 0 0 3 3l4-9V2H5.72a2 2 0 0 0-2 1.7l-1.38 9a2 2 0 0 0 2 2.3zm7-13h2.67A2.31 2.31 0 0 1 22 4v7a2.31 2.31 0 0 1-2.33 2H17\"\n\t\t\t\t\t><\/path>\n\t\t\t\t<\/svg>\n\t\t\t\t<span class=\"sr-only\"> No <\/span>\n\t\t\t<\/button>\n\t\t<\/div>\n\t<\/header>\n\n\t<div class=\"wth-form hidden\">\n\t\t<div class=\"wth-form-wrapper\">\n\t\t\t<div class=\"wth-title\"><\/div>\n\t\t\t<textarea class=\"wth-message\"><\/textarea>\n\t\t\t<input type=\"button\" name=\"wth-submit\" class=\"wth-btn wth-btn-submit\" id=\"wth-submit\" \/>\n\t\t\t<input type=\"button\" class=\"wth-btn wth-btn-cancel\" value=\"Cancel\" \/>\n\t\t<\/div>\n\t<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Summary: in this tutorial, you&#8217;ll 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 threading module. By using multiple threads, you [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":4104,"menu_order":5,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-4073","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4073","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/comments?post=4073"}],"version-history":[{"count":0,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4073\/revisions"}],"up":[{"embeddable":true,"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/pages\/4104"}],"wp:attachment":[{"href":"https:\/\/www.pythontutorial.net\/wp-json\/wp\/v2\/media?parent=4073"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}