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?
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!– Luiz Vieira
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.
– Benedito