无法从modu停止线程

2024-05-14 22:08:54 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要能够调用运行线程的停止函数。我尝试了几种方法来达到这个目的,但至今没有成功。我想我需要一个线程id,但不知道如何做到这一点。你知道吗

相关代码: 型号:

import MODULE
class do_it():
    def __init__(self):
        self.on_pushButton_start_clicked()
        return

    def on_pushButton_start_clicked(self):
        self.Contr = MODULE.Controller()
        self.Contr.start()

    def on_pushButton_stop_clicked(self):
        if self.Contr:
            self.Contr.stop()
            self.Contr = None
        return

模块:

import thread
class Controller():
    def __init__(self):
        self.runme = False

    def start(self):
        """Using this to start the controllers eventloop."""
        thread.start_new_thread(self._run, ())

    def stop(self):
        """Using this to stop the eventloop."""
        self.runme = False

    def _run(self):
        """The actual eventloop"""
        self.runme = True

我想我的问题就在这里…
我的控制器:

    """I want to use this to control my model, that in turn controls the module"""

    def start_module():
        start=do_it().on_pushButton_start_clicked()
    return 'Ran start function'

    def stop_module():
        stop=do_it().on_pushButton_stop_clicked()
    return 'Ran stop function'

Tags: toselfreturnondefitthisdo
1条回答
网友
1楼 · 发布于 2024-05-14 22:08:54

关于thread模块,文档是这么说的:

This module provides low-level primitives for working with multiple threads […] The threading module provides an easier to use and higher-level threading API built on top of this module.

此外,一旦线程启动,就无法停止或终止它。This所以问题会更详细,并展示如何使用threading.Event实现可停止线程。你知道吗

唯一的区别是daemon thread,它将在主程序存在时自动终止。你知道吗

相关问题 更多 >

    热门问题