解决python父线程关闭后子线程不关闭问题


Posted in Python onApril 25, 2020

我们都知道,python可以通过threading module来创建新的线程,然而在创建线程的线程(父线程)关闭之后,相应的子线程可能却没有关闭,这可能是因为代码中没有使用setDaemon(True)函数。

接下来,使用一个例子来说明:

import threading

def prt_hello() :
  while 1 :
    print 'hello'

if __name__ == '__main__' :
  t = threading.Thread(target=prt_hello)
  t.setDaemon(True)
  t.start()

我们需要把setDaemon函数放在start函数前面,不然它是不给通过的,并且返回'cannot set daemon status of active thread‘

补充知识:Python 多线程的退出/停止的一种是实现思路

在使用多线程的过程中,我们知道,python的线程是没有stop/terminate方法的,也就是说它被启动后,你无法再主动去退出它,除非主进程退出了,注意,是主进程,不是线程的父进程.

一个比较合理的方式就是把原因需要放到threading.Thread的target中的线程函数,改写到一个继承类中,下面是一个实现例子

import threading
import time
import os
 
# 原本需要用来启动的无线循环的函数
def print_thread():
  pid = os.getpid()
  counts = 0
  while True:
    print(f'threading pid: {pid} ran: {counts:04d} s')
    counts += 1
    time.sleep(1)
 
# 把函数放到改写到类的run方法中,便可以通过调用类方法,实现线程的终止
class StoppableThread(threading.Thread):
 
  def __init__(self, daemon=None):
    super(StoppableThread, self).__init__(daemon=daemon)
    self.__is_running = True
    self.daemon = daemon
 
  def terminate(self):
    self.__is_running = False
 
  def run(self):
    pid = os.getpid()
    counts = 0
    while self.__is_running:
      print(f'threading running: {pid} ran: {counts:04d} s')
      counts += 1
      time.sleep(1)
 
 
def call_thread():
  thread = StoppableThread()
  thread.daemon = True
  thread.start()
 
  pid = os.getpid()
  counts = 0
  for i in range(5):
    print(f'0 call threading pid: {pid} ran: {counts:04d} s')
    counts += 2
    time.sleep(2)
  # 主动把线程退出
  thread.terminate()
 
 
if __name__ == '__main__':
  call_thread()
  print(f'==========call_thread finish===========')
  counts = 0
  for i in range(5):
    counts += 1
    time.sleep(1)
    print(f'main thread:{counts:04d} s')

以上这篇解决python父线程关闭后子线程不关闭问题就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持三水点靠木。

Python 相关文章推荐
Python使用bs4获取58同城城市分类的方法
Jul 08 Python
Python实现HTTP协议下的文件下载方法总结
Apr 20 Python
python logging 日志轮转文件不删除问题的解决方法
Aug 02 Python
python 与GO中操作slice,list的方式实例代码
Mar 20 Python
Python tornado队列示例-一个并发web爬虫代码分享
Jan 09 Python
python批量设置多个Excel文件页眉页脚的脚本
Mar 14 Python
Python列表元素常见操作简单示例
Oct 25 Python
python将音频进行变速的操作方法
Apr 08 Python
keras自动编码器实现系列之卷积自动编码器操作
Jul 03 Python
Python logging日志模块 配置文件方式
Jul 12 Python
matplotlib基础绘图命令之errorbar的使用
Aug 13 Python
Pytorch 扩展Tensor维度、压缩Tensor维度的方法
Sep 09 Python
Python标准库:内置函数max(iterable, *[, key, default])说明
Apr 25 #Python
python except异常处理之后不退出,解决异常继续执行的实现
Apr 25 #Python
python 追踪except信息方式
Apr 25 #Python
Python实现捕获异常发生的文件和具体行数
Apr 25 #Python
python IDLE添加行号显示教程
Apr 25 #Python
IDLE下Python文件编辑和运行操作
Apr 25 #Python
python 字典item与iteritems的区别详解
Apr 25 #Python
You might like
超强分页类2.0发布,支持自定义风格,默认4种显示模式
2007/01/02 PHP
linux系统下php安装mbstring扩展的二种方法
2014/01/20 PHP
PHP实现文件下载详解
2014/11/27 PHP
PHP查询并删除数据库多列重复数据的方法(利用数组函数实现)
2016/02/23 PHP
PHP Post获取不到非表单数据的问题解决办法
2018/02/27 PHP
FireFox与IE 下js兼容触发click事件的代码
2008/11/20 Javascript
JavaScript 学习技巧
2010/02/17 Javascript
最新的10款jQuery内容滑块插件分享
2011/09/18 Javascript
jQuery拖动图片删除示例
2013/05/10 Javascript
导航跟随滚动条置顶移动示例代码
2013/09/11 Javascript
jQuery如何实现点击页面获得当前点击元素的id或其他信息
2014/01/09 Javascript
用unescape反编码得出汉字示例
2014/04/24 Javascript
jQuery实现给页面换肤的方法
2015/05/30 Javascript
jQuery实现下拉框左右移动(全部移动,已选移动)
2016/04/15 Javascript
老生常谈onBlur事件与onfocus事件(js)
2016/07/09 Javascript
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
详解webpack引入第三方库的方式以及注意事项
2019/01/15 Javascript
Vue中使用better-scroll实现轮播图组件
2020/03/07 Javascript
python实现堆栈与队列的方法
2015/01/15 Python
python fabric实现远程部署
2017/01/05 Python
PyQt5每天必学之带有标签的复选框
2018/04/19 Python
使用python制作一个解压缩软件
2019/11/13 Python
Python解释器及PyCharm工具安装过程
2020/02/26 Python
Python numpy多维数组实现原理详解
2020/03/10 Python
Python Django中的STATIC_URL 设置和使用方式
2020/03/27 Python
python多进程 主进程和子进程间共享和不共享全局变量实例
2020/04/25 Python
美国最便宜的旅游网站:CheapTickets
2017/07/09 全球购物
PatPat香港:婴童服饰和亲子全家装在线购物
2020/09/27 全球购物
职业教育毕业生求职信
2013/11/09 职场文书
2014年施工员工作总结
2014/11/18 职场文书
2014年创先争优工作总结
2014/12/11 职场文书
无婚姻登记记录证明
2015/06/18 职场文书
医生行业员工的辞职信
2019/06/24 职场文书
pandas中对文本类型数据的处理小结
2021/11/01 Python
python全面解析接口返回数据
2022/02/12 Python
vue项目配置sass及引入外部scss文件
2022/04/14 Vue.js