python多线程守护线程_Python守护程序线程

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.

要开始本教程,您应该具有有关线程的基本知识。 基本上有两种类型的线程。 一种是守护线程。 另一个是非守护线程。

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 time


def 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 time


def 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)

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

参考: 官方文档

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

python多线程守护线程

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值