0

I need to be able to call a stop funtion of a running thread. I tried several ways to achieve this but so far no luck. I think I need a thread id but have no idea how this is done.

relevant code: model:

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

module:

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 think my issue lies here...
my controller:

    """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'
Ivo
  • 1
  • 1

1 Answers1

0

Regarding the thread module, this is what the docs say:

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.

Furthermore, there is no way to stop or kill a thread once it's started. This SO question goes further into detail and shows how to use a threading.Event to implement a stoppable thread.

The only difference is a daemon thread, which will be killed automatically when your main program exists.

Community
  • 1
  • 1
DocZerø
  • 8,037
  • 11
  • 38
  • 66
  • Thank you for your input! I read the doc and tried to use threading (unsuccessfull due to lack of my knowledge and experience). I came across a simular question as the one you sugest. I think the top part of the most voted answer is what would work for me; However I have no idea how to implement it in my situation. what should I pass as an arg when calling StoppableThread(threading.Thread)? – Ivo Jul 24 '16 at 11:15