Solved: pause and resume threads python

The main problem related to pause and resume threads is that they can cause race conditions.


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

This line imports the threading, time, and sys modules.
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

Events

There are many events in Python that can be used for programming purposes. Some of the more common ones include:

• Keyboard input: When a key is pressed, Python will execute the code associated with that key.
• Timers: Python has built-in timers that can be used for timing operations or other tasks.
• File input/output: When a file is opened, Python will execute any code associated with that file.

Wait Event

A wait event is a Python keyword that allows you to suspend execution of a block of code until a certain condition is met.

Related posts:

Leave a Comment