Rešeno: pauziraj i nastavi niti python

Posljednje ažuriranje: 09/11/2023

Glavni problem vezan za pauziranje i nastavak niti je to što one mogu uzrokovati uvjete utrke.

I am trying to write a program in Python that will pause and resume threads. I have found some code online, but it doesn't seem to work. Here is the code I have so far:
<code>import threading
import time
import sys

class StoppableThread(threading.Thread):

    def __init__(self):
        super(StoppableThread, self).__init__()

        self._stop = threading.Event()

    def stop(self):
        self._stop.set()

    def stopped(self):
        return self._stop.isSet()

    def run(self):
        while not self.stopped():  # &lt;-- this is never true! (?)  &lt;--- This is because you never call the stop method!  You need to add a call to it somewhere in your code!   --&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;&gt;  Add a line here that calls the stop method of your thread object!!! &lt;---   &lt;---   &lt;---   &lt;---   &lt;---   &lt;---   &lt;---   &lt;---     --^--^--^--^--^--^--^--^-v-v-v-v-v-v-v-v----^^----^^----^^----^^----^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------| | | | | | |-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?)  # ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^------ You don't need this either, because if the while loop above exits, then this program will exit too! ---vvvvvvvvvvvvvvvvvvvv---------- v v v v v v v v -----------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?)  # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?)  # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys</code>

import threading
import time
import sys

Ova linija uvozi module threading, time i sys.
class StoppableThread(threading.Thread):

def __init__(self):
super(StoppableThread, self).__init__()

self._stop = threading.Event()

def stop(self):
self._stop.set()

def stopped(self):
return self._stop.isSet()

def run(self):
while not self.stopped(): # <-- this is never true! (?) <--- This is because you never call the stop method! You need to add a call to it somewhere in your code! -->>>>>>>>> Add a line here that calls the stop method of your thread object!!! <--- <--- <--- <--- <--- <--- <--- --^--^--^--^--^--^--^--^-v-v-v-v-v-v-v----^^----^^----^^----^^----^^------^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^----------------------------| | | | | | |-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|-----------------------------------------|-------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?) # ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^----- This line is unnecessary since you are exiting anyway!!! ^------ You don't need this either, because if the while loop above exits, then this program will exit too! ---vvvvvvvvvvvvvvvvvvvv---------- v v v v v v v v -----------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?) # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys.exit() # exit cleanly if we are told to stop... (?) # --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------# do stuff here... print "Doing stuff..." time.sleep(1) print "Done doing stuff." sys

Događanja

Postoji mnogo događaja u Pythonu koji se mogu koristiti u programske svrhe. Neki od češćih uključuju:

• Unos sa tastature: Kada se pritisne taster, Python će izvršiti kod povezan sa tim tasterom.
• Tajmeri: Python ima ugrađene tajmere koji se mogu koristiti za operacije mjerenja vremena ili druge zadatke.
• Ulaz/izlaz datoteke: Kada se datoteka otvori, Python će izvršiti bilo koji kod povezan sa tom datotekom.

Wait Event

Događaj čekanja je ključna riječ Python-a koja vam omogućava da obustavite izvršavanje bloka koda dok se ne ispuni određeni uvjet.

Slični postovi: