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

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多线程守护线程

python多线程守护线程_Python守护程序线程相关推荐

  1. python多线程数据交互_python 多线程 通信

    一篇文章搞定Python多进程(全) 公众号:pythonislover 前面写了三篇关于python多线程的文章,大概概况了多线程使用中的方法,文章链接如下: 一篇文章搞懂Python多线程简单实现 ...

  2. python多线程并行编程_Python并行编程(二):基于线程的并行

    1.介绍 软件应用中使用最广泛的并行编程范例是多线程.通常一个应用有一个进程,分成多个独立的线程,并行运行.互相配合,执行不同类型的任务. 线程是独立的处理流程,可以和系统的其他线程并行或并发地执行. ...

  3. python多线程网络编程_python网络编程之线程

    一 .背景知识 1.进程 之前我们已经了解了操作系统中进程的概念,程序并不能单独运行,只有将程序装载到内存中,系统为它分配资源才能运行,而这种执行的程序就称之为进程.程序和进程的区别就在于:程序是指令 ...

  4. python结束线程_python终止线程

    可以通过以下方式来终止线程:通过抛出异常来终止进程 通过一个终止标志来终止进程 使用traces来终止进程 使用多线程模型来终止进程 通过将进程设置为deamon来终止进程 使用隐藏属性_stop() ...

  5. Python 多线程总结(2)— 线程锁、线程池、线程数量、互斥锁、死锁、线程同步

    主要介绍使用 threading 模块创建线程的 3 种方式,分别为: 创建 Thread 实例函数 创建 Thread 实例可调用的类对象 使用 Thread 派生子类的方式 多线程是提高效率的一种 ...

  6. python结束线程池正在运行的线程_python之线程与线程池

    #进程是资源分配的最小单位,线程是CPU调度的最小单位.每一个进程中至少有一个线程.#传统的不确切使用线程的程序称为只含有一个线程或单线程程序,而可以使用线程的程序被称为多线程程序,在程序中使用一个线 ...

  7. python获取当前线程_Python爬虫(线程,进程)

    第一章   线程的使用 并发:指的是任务数多余cpu核数 并行:指的是任务数小于等于cpu核数,即任务真的是一起执行的 1.线程的概念 线程就是在程序运行过程中,执行程序代码的一个分支,每个运行的程序 ...

  8. python多线程的使用(导入线程模块、创建子线程任务、启动子线程任务、获取当前执行的线程号)

    1. 导入线程模块 #导入线程模块 import threading 2. 线程类Thread参数说明 Thread([group [, target [, name [, args [, kwarg ...

  9. python多线程有用吗_Python多线程理解

    前言 在写python爬虫的时候遇到了多线程,使用多线程的目的是降低抓取时间.接着我接触了一些IO概念,IO就是Input和Ouput,数据进出CPU的意思. 数据从网线或网卡进入CPU算Input( ...

最新文章

  1. java类引用接口的注释_java – 在接口类型上使用注释有什么好处?
  2. 语义分割--Global Deconvolutional Networks for Semantic Segmentation
  3. delphi 图像处理 图像左旋右旋
  4. java监控activemq,ActiveMQ与Spring整合-监听消息
  5. cisco网络故障处理手册
  6. JXOI2018做题笔记
  7. 太疯狂, 2021年涨幅第一,房价几天翻一倍, 全球炒房客都来了
  8. python开发环境搭建---pyenv安装python3.5.2
  9. 安义县农业结构调整-农业大健康·林裕豪:从玉农业谋定基地
  10. 数据库部分重点内容回顾
  11. HTML5新增的客户端校验
  12. Feign 集成 Hystrix实现不同的调用接口不同的设置
  13. linux hid 输入设备 在window上需要额外驱动?,什么是HID兼容设备?Win10缺少HID兼容的触摸屏驱动咋办?...
  14. Mysql数据库读写分离的实现
  15. HTML+CSS学习笔记
  16. 阿里云服务器购买价格表:国内和国外地域云服务器活动报价表
  17. 淘宝省钱_如何省钱和组装自己的电路板
  18. KVM虚拟机如何新增一块磁盘?
  19. 交通期刊JCR(2020)
  20. python dataframe 写入到doc文件_将Python Pandas DataFrame写入Word文档

热门文章

  1. 不支持对系统目录进行即席更新
  2. 如何用VB.Net创建一个三层的数据库应用程序
  3. [转载] python 生成器读取文件
  4. verilog之用户定义原语UDP详细解释
  5. 使用 RemObjects SDK 建立 WebService 应用
  6. 05 jQuery的DOM操作
  7. Jenkins创建新任务
  8. dokcer 容器启动报错
  9. 谈谈我们的学习和我们的Blog
  10. ROS☞rosbag/rostopic消息记录、回放、转.txt