Gelöst: Python-Threads pausieren und fortsetzen

Letzte Aktualisierung: 09/11/2023

Das Hauptproblem im Zusammenhang mit dem Anhalten und Fortsetzen von Threads besteht darin, dass sie Race-Conditions verursachen können.

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

Diese Zeile importiert die Module threading, time und 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

VERANSTALTUNGEN

Es gibt viele Ereignisse in Python, die für Programmierzwecke verwendet werden können. Einige der häufigeren sind:

• Tastatureingabe: Wenn eine Taste gedrückt wird, führt Python den dieser Taste zugeordneten Code aus.
• Timer: Python hat eingebaute Timer, die für Timing-Operationen oder andere Aufgaben verwendet werden können.
• Dateieingabe/-ausgabe: Wenn eine Datei geöffnet wird, führt Python jeglichen mit dieser Datei verknüpften Code aus.

Ereignis warten

Ein Warteereignis ist ein Python-Schlüsselwort, mit dem Sie die Ausführung eines Codeblocks aussetzen können, bis eine bestimmte Bedingung erfüllt ist.

Zusammenhängende Posts: