linux 守護進程,python多線程守護線程_Python守護程序線程

 2023-11-19 阅读 24 评论 0

摘要:python多線程守護線程In this tutorial we will be learning about Python Daemon Thread. In our previous tutorial we learned about Python getattr() function. 在本教程中,我們將學習Python Daemon Thread。 在上一教程中,我們了解了Python getattr࿰

python多線程守護線程

In this tutorial we will be learning about Python Daemon Thread. In our previous tutorial we learned about Python getattr() function.

在本教程中,我們將學習Python Daemon Thread。 在上一教程中,我們了解了Python getattr()函數。

Python守護程序線程 (Python Daemon Thread)

To start with this tutorial, you should have basic knowledge about threads. Basically there are two types of thread. One is daemon thread. Another is non-daemon thread.

linux 守護進程、 要開始本教程,您應該具有有關線程的基本知識。 基本上有兩種類型的線程。 一種是守護線程。 另一個是非守護線程。

While a non-daemon thread blocks the main program to exit if they are not dead. A daemon thread runs without blocking the main program from exiting. And when main program exits, associated daemon threads are killed too.

當非守護進程線程阻塞主程序時,如果它們沒有死,它們將退出。 守護程序線程在運行時不會阻止主程序退出。 當主程序退出時,關聯的守護程序線程也會被殺死。

Python守護程序線程示例 (Python daemon thread example)

We have a simple program where we are creating two threads. One of them will take longer time to execute because we have added sleep of 2 seconds. Let’s run the below program and observe the output.

我們有一個簡單的程序,在其中創建兩個線程。 其中之一將花費更長的時間執行,因為我們增加了2秒的睡眠時間。 讓我們運行以下程序并觀察輸出。

import threading
import timedef print_work_a():print('Starting of thread :', threading.currentThread().name)time.sleep(2)print('Finishing of thread :', threading.currentThread().name)def print_work_b():print('Starting of thread :', threading.currentThread().name)print('Finishing of thread :', threading.currentThread().name)a = threading.Thread(target=print_work_a, name='Thread-a')
b = threading.Thread(target=print_work_b, name='Thread-b')a.start()
b.start()

主線程和守護線程、You will get output like below.

您將獲得如下輸出。

Starting of thread : Thread-a
Starting of thread : Thread-b
Finishing of thread : Thread-b
Finishing of thread : Thread-a

So both the threads executed and then main thread exits and terminates the program.

因此,兩個執行線程然后主線程退出并終止程序。

Now we will make Thread a as a daemon thread. Then you will see the difference in output. So, let’s edit the previous code as following.

多線程線程池。 現在,我們將Thread a作為守護線程。 然后,您將看到輸出的差異。 因此,讓我們如下編輯先前的代碼。

import threading
import timedef print_work_a():print('Starting of thread :', threading.currentThread().name)time.sleep(2)print('Finishing of thread :', threading.currentThread().name)def print_work_b():print('Starting of thread :', threading.currentThread().name)print('Finishing of thread :', threading.currentThread().name)a = threading.Thread(target=print_work_a, name='Thread-a', daemon=True)
b = threading.Thread(target=print_work_b, name='Thread-b')a.start()
b.start()

Notice the extra argument daemon=True while creating Thread a. This is how we specify a thread as daemon thread. Below image shows the output by the program now.

在創建線程a時,請注意額外的參數daemon=True 。 這就是我們將線程指定為守護程序線程的方式。 下圖顯示了程序現在的輸出。

Notice that program exits even though daemon thread was running.

請注意,即使守護程序線程正在運行,程序也會退出。

當守護程序線程有用時 (When Daemon Threads are useful)

django圖表。In a big project, some threads are there to do some background task such as sending data, performing periodic garbage collection etc. It can be done by non-daemon thread. But if non-daemon thread is used, the main thread has to keep track of them manually. However, using daemon thread the main thread can completely forget about this task and this task will either complete or killed when main thread exits.

在大型項目中,一些線程在那里執行一些后臺任務,例如發送數據,執行定期垃圾回收等。它可以由非守護進程線程完成。 但是,如果使用了非守護程序線程,則主線程必須手動跟蹤它們。 但是,使用守護程序線程,主線程可以完全忘記此任務,并且該任務將在主線程退出時完成或終止。

Note that you should use daemon thread only for non essential tasks that you don’t mind if it doesn’t complete or left in between.

請注意,您僅應將守護程序線程用于非必需的任務,如果它們沒有完成或介于兩者之間,則不必理會。

Reference: Official Documentation

python socket? 參考: 官方文檔

翻譯自: https://www.journaldev.com/16152/python-daemon-thread

python多線程守護線程

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/183232.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息