子进程 子线程的关闭

子进程的关闭

from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton
from multiprocessing import Process, Queue
import threading
import time
 
class MyProcess(Process):
    def __init__(self, q, data):
        super().__init__()
        self.q = q
        self.data = data
 
    def run(self):
        time.sleep(20)
        print('子进程开始put数据')
        self.data.append('123')
        self.q.put(self.data)
 
class MainWindow(QMainWindow):
    def __init__(self):
        super().__init__()
        self.q = Queue()
        self.data = ['111', '222', '333']  # 要传递的列表
 
        self.button = QPushButton('启动子进程', self)
        self.button.clicked.connect(self.start_child_process)
 
    def start_child_process(self):
        def run_child_process():
            p = MyProcess(self.q, self.data)
            p.start()
            p.join()
            p.close()  # 关闭子进程
 
            # 子进程完成后的逻辑
            result = self.q.get()
            print('主进程获取Queue数据:', result)
            self.q.close()  # 关闭队列
 
        thread = threading.Thread(target=run_child_process)
        thread.start()
 
        # 允许GUI事件循环继续处理其他事件
        while thread.is_alive():
            QApplication.processEvents()
 
if __name__ == '__main__':
    app = QApplication([])
    window = MainWindow()
    window.show()
    app.exec_()

子线程的关闭

import inspect
import ctypes
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)

停止线程

stop_thread(myThread)

相关推荐

  1. 进程 线关闭

    2024-01-16 16:54:04       59 阅读
  2. 进程线关系

    2024-01-16 16:54:04       45 阅读
  3. PyQt线处理业务事件

    2024-01-16 16:54:04       55 阅读
  4. 原语,原线安全

    2024-01-16 16:54:04       51 阅读
  5. js开启线及其使用

    2024-01-16 16:54:04       42 阅读
  6. qt 线更新ui举例

    2024-01-16 16:54:04       25 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-16 16:54:04       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-16 16:54:04       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-16 16:54:04       82 阅读
  4. Python语言-面向对象

    2024-01-16 16:54:04       91 阅读

热门阅读

  1. Matlab 建文件夹保存本次仿真图表数据和参数

    2024-01-16 16:54:04       55 阅读
  2. 【JVM】字节码文件的组成

    2024-01-16 16:54:04       55 阅读
  3. Android 13 默认讯飞输入法

    2024-01-16 16:54:04       72 阅读
  4. 大模型相关资料

    2024-01-16 16:54:04       58 阅读
  5. YOLOv8自带的追踪算法简单使用教程

    2024-01-16 16:54:04       53 阅读
  6. 用友U8录请购单时调不到名字

    2024-01-16 16:54:04       52 阅读
  7. org.openjdk.jmh 的 pom 引用

    2024-01-16 16:54:04       53 阅读
  8. 图片转换成png格式上传

    2024-01-16 16:54:04       55 阅读
  9. 开放签电子签章加入渠成开源社区

    2024-01-16 16:54:04       56 阅读