How to create a pause method and one that resumes a Thread?

Asked

Viewed 753 times

1

The following script shows the x variable every 3 seconds, while the xx class, which is running on a thread, increases the x value every second. I would like when the value of x reaches 13, the thread where class xx is being executed to stop.

from threading import Thread
from time import sleep

class xx(Thread):
    def __init__(self):
        Thread.__init__(self)

    def run(self):
        global x
        while True:
            x+=1
            sleep(1)

    def stop(self):
        #O que botar aqui para parar a execução da thread?

    def play(self):
        #O que botar aqui para iniciar a thread depois de pausada?



x=0

instx=xx()
instx.start()

while True:
    sleep(3)
    print(x)
    if x==13:
        instx.stop()

But I don’t know what to put in stop(self) and play(self) methods to pause and resume thread execution. How do I do this?

  • 1

    In one of the answers I gave just in a question from you, i do exactly what you want here: http://answall.com/a/178303/73 Did you ever study the code at that time? Because it seems not, right? Well, the answer is simple: use a control variable in your loop while. Another thing to think about: "stop" is very different from "pause". Good luck!

  • It’s true. I confess that at the time I didn’t quite understand the whole script because I was starting my study on threads. I see it more clearly now. And thanks for the tip.

1 answer

3


There is not much magic possible, apart from some data care shared between the various threads.

In your thread daughter, the only code running is the one in the "run" function and all other methods are actually code executed on the main thread, which creates the daughter threads.

So, anything you want to do that has an effect on what’s running on the daughter thread, has to be done on the code that runs on the thread -

Once you got there one while True:, ie, a code that loops, in a line of that while you must either check, or call a function that checks, changed data in the main function.

With this, and with the use of Queue, Locks, Events and Semaphores, described in https://docs.python.org/3/library/threading.html you can control bi-directional communication effectively between a main thread and a secondary thread, or between the main thread and a set (pool) of coordinate secondary threas.

In particular, "Lock" type objects can be "the right way" to do what you want to do with "stop" and "play" methods there. But to answer simply, what you would do is set up an object like "Event" - it’s something very simple from the threading module, which has a call wait that blocks, and only returns the execution when the object is activated with the call to .set(), in some other thread.

from threading import Thread
from threading import Event
from time import sleep

class xx(Thread):
    def __init__(self):
        self._active = Event()
        self._active.set()
        Thread.__init__(self)

    def run(self):
        global x
        while True:
            self._active.wait()
            x+=1
            sleep(1)

    def stop(self):
        self.active.clear()

    def play(self):
        self.active.set()

Browser other questions tagged

You are not signed in. Login or sign up in order to post.