Python的子线程和子进程是如何手动结束的?
作者:l198738655 发布时间:2022-09-08 18:54:41
标签:Python,子线程,子进程
如何结束Python的子线程和子进程
结束子线程的方法:
这个是搬运其他大神的代码,鄙人也不知道原理,反正拿来主义,暂时没发现什么缺点,先用着再说。
import inspect
import ctypes
import threading
from time import sleep
def serial_read():
while True:
print("春哥纯爷们!")
sleep(1)
def _async_raise(tid, exctype):
"""raises the exception, performs cleanup if needed"""
tid = ctypes.c_long(tid)
if not inspect.isclass(exctype):
exctype = type(exctype)
res = ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, ctypes.py_object(exctype))
if res == 0:
raise ValueError("invalid thread id")
elif res != 1:
# """if it returns a number greater than one, you're in trouble,
# and you should call it again with exc=NULL to revert the effect"""
ctypes.pythonapi.PyThreadState_SetAsyncExc(tid, None)
raise SystemError("PyThreadState_SetAsyncExc failed")
def stop_thread(thread):
_async_raise(thread.ident, SystemExit)
def Air():
ords=0
myThread = threading.Thread(target=serial_read)
myThread.start()
while True:
ords+=1
if ords==10:
stop_thread(myThread)
print("停止子线程")
break
sleep(1)
if __name__ == '__main__':
Air()
下面是结束子进程的方法:
import inspect
import ctypes
from time import sleep
from multiprocessing import Process
def serial_read():
while True:
print("春哥纯爷们!")
sleep(1)
def Air():
ords=0
myThread = Process(target=serial_read)
myThread.start()
while True:
ords+=1
if ords==10:
myThread.terminate()
print("停止子进程")
break
sleep(1)
if __name__ == '__main__':
Air()
这里说一下如果用类的话要如何写,想结束子进程或者子线程就需要拿到进程对象或者线程对象,但是类中没办法实现创建一个类属性的方式然后用self.×××的方式来在其他类方法中调用,这时候就创建一个类属性list,然后创建好子进程或者子线程后把对象赋值给这个list,再在类的其他方法中调用这个list中的元素,就拿到了子进程或者子线程的对象了。
例如:
def startss(self,a1,b1,c1,under,rough,blue,among):
# 创建新线程
p1=threading.Thread(target=self.line01,args=(a1,b1,under,rough,)) #必须加,号
p2=threading.Thread(target=self.line02,args=(a1,c1,under,blue,))
p3=Process(target=self.Process01,args=(under,rough,blue,)) #计算进程
#among是类属性list容器
among.append(p1)
among.append(p2)
among.append(p3)
# 开启新线程
p1.start()
p2.start()
#开启计算用进程
p3.start()
参考资料:https://www.jb51.net/article/185867.htm
python 多进程如何终止或重启子进程
来源:https://blog.csdn.net/l198738655/article/details/117902289


猜你喜欢
- Go语言中,一般方法接收者和接口方法接收者有一定区别在一般方法中若定义的接收者是值,可以使用值或者指针进行调用;若定义的接收者是指针,可以使
- 权限及设计数据库用户管理使用SQLyog 创建用户,并授予权限演示基本命令/* 用户和权限管理 */ ------------------用
- 抢票脚本,python +splinter自动刷新抢票,可以成功抢到(依赖自己的网络环境太厉害,还有机器的好坏),但是感觉不是很完美。有大神
- Selenium Python 提供了一个简单的API 便于我们使用 Selenium WebDriver编写 功能/验收测试。 通过Sel
- 前言以前版本的 Celery 需要一个单独的库(django-celery)来与 Django 一起工作,但从 3.1 开始不再是这种情况。
- 本文实例为大家分享了python自动发送邮件的具体代码,供大家参考,具体内容如下#coding=utf8 ''&
- 之前做了一个淘宝客返利微信公众号,后来很多人提到过微信返利机器人,现在微信助手开发好了,可以通过微信助手接口功能实现微信返利机器人。流程如下
- 如果值没有重复的情况,可以先用array_flip()来交换键和值,然后krsort(),最后再array_flip()交换回来,就可以比较
- 为了优化OceanBase的query timeout设置方式,特调研MySQL关于timeout的处理,记录如下。 mysql> s
- 查看表空间的名称及大小代码如下:SQL>select t.tablespace_name, round(sum(bytes/(1024
- pythonDES加密与解密以及hex输出和bs64格式输出具体代码如下所示:import pyDesimport base64Key =
- 第一种方法:用军哥的一键修改LNMP环境下MYSQL数据库密码脚本一键脚本肯定是非常方便。具体执行以下命令:wget http://soft
- 本文实例讲述了Go语言的队列和堆栈实现方法。分享给大家供大家参考。具体如下:golang,其实我的实现是利用container/list包实
- 脚本需求:每天备份mysql数据库,保留7天的脚本。存放在/opt/dbbak目录中。脚本名称为database_xxxx-xx-xx.sq
- 本文实例讲述了Python wxPython库消息对话框MessageDialog用法。分享给大家供大家参考,具体如下:消息对话框即我们平时
- 由于psutil已更新到3.0.1版本,最新的代码如下:#!/usr/bin/env pythonimport osimport timei
- 如果要在某个数组中删除一个元素,可以直接用的unset,但今天看到的东西却让我大吃一惊<?php$arr = array('a
- MySQL各版本,对于add Index的处理方式是不同的,主要有三种:(1)Copy Table方式这是InnoDB最早支持的创建索引的方
- 主要代码是参考:https://github.com/SoulDGXu/NLPVisualizationSystem/tree/master
- 逻辑判断与逻辑语句对于─件事情正确与否(真假的判断) √ X根据判断的结果做不同的事情,就是我们的逻辑业务对于条件满足的判断语句,就是条件语