0

I already asked my question but It was too complicated to understand. So it is the question in a very simple way. What I want is to have flow control on a loop with thread in a class in python3. Suppose this code:

import threading
from time import sleep


class someclass(threading.Thread):

    def __init__(self):
        super(someclass, self).__init__()
        self.stop_event = False

    def poll(self):
        print("your main function is running")
        for i in range (0,50):
            print("i=",i)
            sleep(0.5)

    def stop(self):
        print("stopping the thread")
        self.stop_event = True


    def stopped(self):
        return self.stop_event

    def run(self):
        print("stop_event: ",self.stop_event)
        while not self.stopped():
            for i in range (0,50):
                print("i=",i)
                sleep(0.5)

if __name__=='__main__':
    thr=someclass()
    print("starting the thread #1")
    thr.start()
    sleep(10)
    print("stopping the thread #1")
    thr.stop()
    sleep(10)
    thr2=someclass()
    print("starting the thread #2")
    thr2.start()
    sleep(10)
    print("stopping the thread #2")
    thr2.stop()
    sleep(10)
    print("Exiting the whole program")

so the output would be something like this:

starting the thread #1
stop_event:  False
i= 0
i= 1
...
i= 17
i= 18
i= 19
stopping the thread #1
stopping the thread
i= 20
i= 21
i= 22
...
i= 38
i= 39
starting the thread #2
stop_event:  False
i= 0
i= 40
i= 1
i= 41
i= 2
i= 42
i= 3

After stopping the thread with thr.stop() it continues to print the i contents but the value of self.stop_event has been set to True. what I want is to control a the looping whenever I want and of course I want it to be in a class not in a main body of my program.

3
  • In run(), the variable self.stopped() is checked only when the whole range(50) has been printed, so it cannot detect the stop event before that. Commented Aug 14, 2017 at 8:22
  • @Gribouillis Thanks that was it. But If I had a blocking object how could I stop that? Commented Aug 14, 2017 at 9:24
  • Blocking methods often accept a timeout parameter that you can use to unblock them now and then. There are some hacks on the web to raise exception in a thread from another thread. This could be a way but portability issues may occur (between platforms/implementations of python/versions of python). There may be other techniques that I don't know. Commented Aug 14, 2017 at 10:16

2 Answers 2

1

While using threadings, you can't use the plain value as the event.

You should use the threading.Event to communicate between threads.

Check the link for detail.

Sign up to request clarification or add additional context in comments.

Comments

0

With the help of @Gribouillis The problem seems to be fixed. I also need to say using a threading.Event object inside a thread which has an endless loop is not a good idea and if you want to start multiple instances of a thread it can be declared many times and don't try to start/stop the only instance of a thread.

import threading
from time import sleep


class someclass(threading.Thread):

    def __init__(self):
        super(someclass, self).__init__()
        self.stop_event = False
        self.i=0
    def poll(self):
        print("your main function is running")
        for i in range (0,50):
            print("i=",i)
            sleep(0.5)

    def stop(self):
        print("stopping the thread")
        self.stop_event = True


    def stopped(self):
        return self.stop_event

    def run(self):
        print("stop_event: ",self.stop_event)
        while not self.stopped():
            #for i in range (0,50):
            print("i=",self.i)
            sleep(0.5)
            self.i+=1

if __name__=='__main__':
    thr=someclass()
    print("starting the thread #1")
    thr.start()
    sleep(10)
    print("stopping the thread #1")
    thr.stop()
    sleep(10)
    thr2=someclass()
    print("starting the thread #2")
    thr2.start()
    sleep(10)
    print("stopping the thread #2")
    thr2.stop()
    sleep(10)
    print("Exiting the whole program")

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.