control a loop in a thread class python - python

I want to create a thread class in python3. I want to control an infinite loop in one of the class function. I want to start or stop this loop in my main function outside the class. suppose this code:
import threading
from time import sleep
class my_thread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self):
super(my_thread, self).__init__()
self._stop_event = threading.Event()
def stop(self):
print("stopping the thread")
self._stop_event.set()
def stopped(self):
return(self._stop_event.is_set())
def run(self):
print("running the thread")
print("start function startt()")
self._stop_event.clear()
self.startt()
def startt(self):
print("it is going to wait forever")
while not self.stopped():
#wait forever, this part is going to run again and again
pass
print("This line never executes")
if __name__+'__main__':
thr=my_thread()
thr.start()
sleep(5)
print("stopping the thread")
thr.stop()
# I cant start the thread and relative while loop again
#thr.start()
print("Exiting the whole program")
But the problem is I can't start the thread twice, so what I want is to have two function for start and stop my while loop. I dont need to stop the thread but I need to control it. It means I want to call stop() and startt() functions for many times whenever needed in my main routine.
Thanks

first, use Queue in my_thread class for manage task( method ) that complete or called by your thread
you can use LIFO Queue , Prioritry Queue ,FIFO Queue detail
second, add a class-method so you can add new method or task into the queue
add below code into your code :
from queue import Queue
# or
# from multiprocessing import Queue
class my_thread(threading.Thread):
queue = Queue()
#classmethod
def add_task(cls,callable_task):
cls.queue.put(callable_task)
def startt(self):
print("it is going to wait forever")
while not self.stopped():
#wait forever, this part is going to run again and again
if not self.queue.empty():
_m = self.queue.get()
# do what ever you want to do with _m
print("This line never executes")
for stop thread
Class my_thread(Threading.Thread)
stop_event = threading.Event()
#classmethod
def stop_thread(cls)
cls.stop_event.set()
def startt(self):
print("it is going to wait forever")
cls = self.__class__
while not cls.stop_event.is_set():
#wait forever, this part is going to run again and again
if not self.queue.empty():
_m = self.queue.get()
# do what ever you want to do with _m
print("This line never executes")
Now call for stop therad
my_thread.stop_thread()
Exapmle
import threading
import time
class my_thread(threading.Thread):
stop_event = threading.Event()
#classmethod
def stop_thread(cls):
cls.stop_event.set()
def run(self):
print("running the thread")
print("start function startt()")
self.__class__.stop_event.clear()
self.startt()
def startt(self):
print("it is going to wait forever")
cls = self.__class__
print cls.stop_event.is_set()
while not cls.stop_event.is_set():
pass
print("This line never executes")
a = my_thread()
a.start()
time.sleep(0.5)
my_thread.stop_thread()
print "\n\n\n"
b = my_thread()
b.start()
time.sleep(0.5)
my_thread.stop_thread()

Related

Killing a python thread

I would like to kill somehow a running thread from my GUI application via setting an event, but I can't use a for loop in my thread so I need some other solution to check the event
I have the following situation.
In a tkinter gui when I click a button I start a thread and set a global variable.
self.thread = StoppableThread(caller=self)
self.thread.start()
is_running = 1
When I next click the button I check the global variable state and if it is already set I send a stop request:
if is_running:
is_running = 0
self.thread.stop()
This is my thread class:
import threading
from time import sleep
class StoppableThread(threading.Thread):
def __init__(self, caller=None):
super(StoppableThread, self).__init__()
self._stop_event = threading.Event()
self.caller = caller
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def run(self) -> None:
while True:
# check for stop
if self.stopped():
break
for i in range(10):
sleep(1)
print('Test')
print('Worker done')
break
Everything works if I change the while to a for loop, but because in this point in my business logic I doesn't have anything to loop for I need to check somehow different the state of the self.stopped(). Is there any way to check it in the while loop?
Or how can I achive this? I tried to use process instead of thread but it wasnt worked because of an error 'process can't pickle tkinter'.
Thank you for any help
This loop will run forever until you set the flag:
def run(self):
while not self.stopped():
sleep(1)
print('Test')
You don't actually need an event. A simple Boolean will do.
FOLLOWUP
Here's an example based on your code that shows how this works:
import threading
from time import sleep
class StoppableThread(threading.Thread):
def __init__(self, caller=None):
super(StoppableThread, self).__init__()
self._stop_event = False
self.caller = caller
def stop(self):
self._stop_event = True
def stopped(self):
return self._stop_event
def run(self) -> None:
while not self.stopped():
sleep(1)
print('Test')
print("exited")
thread = StoppableThread(caller=None)
thread.start()
sleep(5)
thread.stop()
sleep(1)
print("program ending")

Python - Threading - execute simultaneously

I have this example code:
# some imports that I'm not including in the question
class daemon:
def start(self):
# do something, I'm not including what this script does to not write useless code to the question
self.run()
def run(self):
"""You should override this method when you subclass Daemon.
It will be called after the process has been daemonized by
start() or restart().
"""
class MyDaemon(daemon):
def run(self):
while True:
time.sleep(1)
if __name__ == "__main__":
daemonz = MyDaemon('/tmp/daemon-example.pid')
daemonz.start()
def firstfunction():
# do something
secondfunction()
def secondfunction():
# do something
thirdfunction()
def thirdfunction():
# do something
# here are some variables set that I am not writing
firstfunction()
How can I exit from the run(self) function of class "daemon" and going on executing the firstfunction() like written in the last line? I'm a newbie with Python, and I'm trying to learn
# EDIT
I managed to implement the daemon class into the treading class. But I'm in the same situation of first, the script stays in daemon class and doesn't execute the other lines.
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def daemonize(self):
# istructions to daemonize my script process
def run(self):
self.daemonize()
def my_function():
print("MyFunction executed") # never executed
thread = MyThread()
thread.start()
my_function() # the process is successfully damonized but
# this function is never executed
You may use the breakkeyword to exit loops, and continue to the next line. return can be used to exit functions.
class daemon:
def start(self):
self.run()
def run(self):
while True:
break
return
print() # This never executes
If you want MyDaemon to run alongside the rest of your code, you have to make it a process or thread. Code then automatically continues to the next line, while the MyDaemon class (thread/process) runs.
import threading
class MyThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
def run(self):
print("Thread started")
while True:
pass
def my_function():
print("MyFunction executed")
thread = MyThread()
thread.start() # executes run(self)
my_function()
This code produces the following result:
Thread started
MyFunction executed
To make thread a daemon, you can use thread.setDaemon(True). That function must be called before the thread is started:
thread = MyThread()
thread.setDaemon(True)
thread.start()
my_function()

How to stop a thread gracefully with Cntrl+C in Python

I have a class Controller with a method job which I'd like to run at regular intervals using the schedule module. Further, I'd like to have several 'variations' of this job running on separate threads such that they are all can be gracefully interrupted using Cntrl+C. (I do not want to make the threads daemon threads and shut them down abruptly).
Here is what I have so far:
import schedule
import threading
import time
import signal
import sys
class Controller(object):
def __init__(self, name="controller", interval=1):
self.name = name
self.interval = interval
def job(self):
print("My name is {}.".format(self.name))
class ThreadController(threading.Thread):
def __init__(self, *args, **kwargs):
super(ThreadController, self).__init__()
self.controller = Controller(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
schedule.every(self.controller.interval).seconds.do(self.controller.job)
while not self.stopped():
schedule.run_pending()
if __name__ == "__main__":
controller1 = ThreadController(name="foo")
controller2 = ThreadController(name="bar")
try:
controller1.start()
controller2.start()
time.sleep(1000) # This ensures that the execution remains within the 'try' block (for a significant amount of time)
except KeyboardInterrupt:
controller1.stop()
controller2.stop()
The program works, in that for the first 1000 seconds it will alternately print My name is foo. and My name is bar. until Cntrl+C is pressed.
To make the code remain within the try block, however, I am for the time being using time.sleep which is not an elegant solution. What I actually want is to 'wait' until Cntrl+C is pressed. Is there an elegant way to implement this?
(Another thing I tried is the following, after Capture keyboardinterrupt in Python without try-except:
if __name__ == "__main__":
controller1 = ThreadController(name="foo")
controller2 = ThreadController(name="bar")
def signal_handler(signal, frame):
print("Stopping threads and exiting...")
controller1.stop()
controller2.stop()
sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
controller1.start()
controller2.start()
but this seems not to work, as the program keeps printing after Cntrl+C is pressed).
Ctrl+C terminates you main thread and controller1 and controller2 are still running.
You may demonize them
controller1.daemon = True
controller2.daemon = True
before starting. But when you main thread starts these two it will exit and shut down them as well.
So in order to keep it busy run a infinite loop in it
while True:
sleep(0.1)
For the time being I'm going with an infinite loop like the one suggested by Alexey Smirnov. The implementation is slightly different and uses Python's signal:
import schedule
import threading
import time
import signal
import sys
class Controller(object):
def __init__(self, name="controller", interval=1):
self.name = name
self.interval = interval
def job(self):
print("My name is {}.".format(self.name))
class ThreadController(threading.Thread):
def __init__(self, *args, **kwargs):
super(ThreadController, self).__init__()
self.controller = Controller(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
schedule.every(self.controller.interval).seconds.do(self.controller.job)
while not self.stopped():
schedule.run_pending()
def signal_handler(signum, frame):
controller_threads = [thread for thread in threading.enumerate() if isinstance(thread, ThreadController)]
for controller_thread in controller_threads:
print("Stopping {}.".format(controller_thread))
controller_thread.stop()
sys.exit(1)
if __name__ == "__main__":
controller1 = ThreadController(name="foo")
controller2 = ThreadController(name="bar")
signal.signal(signal.SIGINT, signal_handler)
controller1.start()
controller2.start()
while True: time.sleep(0.1) # Keep the main thread alive until interrupted
The advantage of not using daemon threads is that they are not abruptly, but gracefully shut down.

Stopping a thread in python

Is there a way in python to stop a thread? I have a gui with a method which plays 10 second audio files and sends information to GUI window continuously
I am Multithreading because I dont want the GUI to freeze while my files play. I can stop the thread with my current code but takes a while
My code looks something like this :
class MyThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
class Myplayer:
// GUI CODE
def play_button(self, widget):
self.mythread = MyThread(target=self.practice)
self.mythread.start()
def stop_button(self, widget):
if self.mythead.IsAlive:
self.self.stop()
def mplayer(self):
while not self.mythread.stopped:
gobject.idle_add(self.insert_text, "\nPlaying a new file")
subprocess.call(["timidity", "myfile.mid"])
Assuming you want to interrupt your midi file while it's playing if your thread is stopped, you can stop the thread more quickly by using Popen instead of call, and then waiting in a loop for either the process to finish, or for the stop request to come in:
class MyThread(threading.Thread):
"""Thread class with a stop() method. The thread itself has to check
regularly for the stopped() condition."""
def __init__(self, *args, **kwargs):
super(MyThread, self).__init__(*args, **kwargs)
self._stop = threading.Event()
def stop(self):
self._stop.set()
def stopped(self):
return self._stop.isSet()
def run(self):
while not self.stopped:
gobject.idle_add(self.insert_text, "\nPlaying a new file")
p = subprocess.Popen(["timidity", "myfile.mid"])
while p.poll() is None: # This will loop until the process has exited.
if self.stopped:
# Someone set the stop flag. Kill the process and exit
p.terminate()
p.join()
return
time.sleep(.1) # Sleep briefly so we don't hog CPU cycles
Here's an example. We start a single thread which does all the work. After two seconds, we tell it to die by setting the shared Event flag.
The worker thread generally runs in a loop, doing a little processing, then checking the flag. If it's set, then it exits, otherwise the thread does some more work.
source
import time
from threading import *
class WorkerThread(Thread):
def __init__(self, die_flag, *args, **kw):
super(WorkerThread,self).__init__(*args, **kw)
self.die_flag = die_flag
def run(self):
for num in range(3):
if self.die_flag.is_set():
print "{}: bye".format(
current_thread().name
)
return
print "{}: num={}".format(
current_thread().name, num,
)
time.sleep(1)
flag = Event()
WorkerThread(name='whiskey', die_flag=flag).start()
time.sleep(2)
print '\nTELL WORKERS TO DIE'
flag.set()
print '\nWAITING FOR WORKERS'
for thread in enumerate():
if thread != current_thread():
print thread.name,
thread.join()
print
output
whiskey: num=0
whiskey: num=1
TELL WORKERS TO DIE
WAITING FOR WORKERS
whiskey whiskey: bye

How to stop a looping thread in Python?

What's the proper way to tell a looping thread to stop looping?
I have a fairly simple program that pings a specified host in a separate threading.Thread class. In this class it sleeps 60 seconds, the runs again until the application quits.
I'd like to implement a 'Stop' button in my wx.Frame to ask the looping thread to stop. It doesn't need to end the thread right away, it can just stop looping once it wakes up.
Here is my threading class (note: I haven't implemented looping yet, but it would likely fall under the run method in PingAssets)
class PingAssets(threading.Thread):
def __init__(self, threadNum, asset, window):
threading.Thread.__init__(self)
self.threadNum = threadNum
self.window = window
self.asset = asset
def run(self):
config = controller.getConfig()
fmt = config['timefmt']
start_time = datetime.now().strftime(fmt)
try:
if onlinecheck.check_status(self.asset):
status = "online"
else:
status = "offline"
except socket.gaierror:
status = "an invalid asset tag."
msg =("{}: {} is {}. \n".format(start_time, self.asset, status))
wx.CallAfter(self.window.Logger, msg)
And in my wxPyhton Frame I have this function called from a Start button:
def CheckAsset(self, asset):
self.count += 1
thread = PingAssets(self.count, asset, self)
self.threads.append(thread)
thread.start()
Threaded stoppable function
Instead of subclassing threading.Thread, one can modify the function to allow
stopping by a flag.
We need an object, accessible to running function, to which we set the flag to stop running.
We can use threading.currentThread() object.
import threading
import time
def doit(arg):
t = threading.currentThread()
while getattr(t, "do_run", True):
print ("working on %s" % arg)
time.sleep(1)
print("Stopping as you wish.")
def main():
t = threading.Thread(target=doit, args=("task",))
t.start()
time.sleep(5)
t.do_run = False
if __name__ == "__main__":
main()
The trick is, that the running thread can have attached additional properties. The solution builds
on assumptions:
the thread has a property "do_run" with default value True
driving parent process can assign to started thread the property "do_run" to False.
Running the code, we get following output:
$ python stopthread.py
working on task
working on task
working on task
working on task
working on task
Stopping as you wish.
Pill to kill - using Event
Other alternative is to use threading.Event as function argument. It is by
default False, but external process can "set it" (to True) and function can
learn about it using wait(timeout) function.
We can wait with zero timeout, but we can also use it as the sleeping timer (used below).
def doit(stop_event, arg):
while not stop_event.wait(1):
print ("working on %s" % arg)
print("Stopping as you wish.")
def main():
pill2kill = threading.Event()
t = threading.Thread(target=doit, args=(pill2kill, "task"))
t.start()
time.sleep(5)
pill2kill.set()
t.join()
Edit: I tried this in Python 3.6. stop_event.wait() blocks the event (and so the while loop) until release. It does not return a boolean value. Using stop_event.is_set() works instead.
Stopping multiple threads with one pill
Advantage of pill to kill is better seen, if we have to stop multiple threads
at once, as one pill will work for all.
The doit will not change at all, only the main handles the threads a bit differently.
def main():
pill2kill = threading.Event()
tasks = ["task ONE", "task TWO", "task THREE"]
def thread_gen(pill2kill, tasks):
for task in tasks:
t = threading.Thread(target=doit, args=(pill2kill, task))
yield t
threads = list(thread_gen(pill2kill, tasks))
for thread in threads:
thread.start()
time.sleep(5)
pill2kill.set()
for thread in threads:
thread.join()
This has been asked before on Stack. See the following links:
Is there any way to kill a Thread in Python?
Stopping a thread after a certain amount of time
Basically you just need to set up the thread with a stop function that sets a sentinel value that the thread will check. In your case, you'll have the something in your loop check the sentinel value to see if it's changed and if it has, the loop can break and the thread can die.
I read the other questions on Stack but I was still a little confused on communicating across classes. Here is how I approached it:
I use a list to hold all my threads in the __init__ method of my wxFrame class: self.threads = []
As recommended in How to stop a looping thread in Python? I use a signal in my thread class which is set to True when initializing the threading class.
class PingAssets(threading.Thread):
def __init__(self, threadNum, asset, window):
threading.Thread.__init__(self)
self.threadNum = threadNum
self.window = window
self.asset = asset
self.signal = True
def run(self):
while self.signal:
do_stuff()
sleep()
and I can stop these threads by iterating over my threads:
def OnStop(self, e):
for t in self.threads:
t.signal = False
I had a different approach. I've sub-classed a Thread class and in the constructor I've created an Event object. Then I've written custom join() method, which first sets this event and then calls a parent's version of itself.
Here is my class, I'm using for serial port communication in wxPython app:
import wx, threading, serial, Events, Queue
class PumpThread(threading.Thread):
def __init__ (self, port, queue, parent):
super(PumpThread, self).__init__()
self.port = port
self.queue = queue
self.parent = parent
self.serial = serial.Serial()
self.serial.port = self.port
self.serial.timeout = 0.5
self.serial.baudrate = 9600
self.serial.parity = 'N'
self.stopRequest = threading.Event()
def run (self):
try:
self.serial.open()
except Exception, ex:
print ("[ERROR]\tUnable to open port {}".format(self.port))
print ("[ERROR]\t{}\n\n{}".format(ex.message, ex.traceback))
self.stopRequest.set()
else:
print ("[INFO]\tListening port {}".format(self.port))
self.serial.write("FLOW?\r")
while not self.stopRequest.isSet():
msg = ''
if not self.queue.empty():
try:
command = self.queue.get()
self.serial.write(command)
except Queue.Empty:
continue
while self.serial.inWaiting():
char = self.serial.read(1)
if '\r' in char and len(msg) > 1:
char = ''
#~ print('[DATA]\t{}'.format(msg))
event = Events.PumpDataEvent(Events.SERIALRX, wx.ID_ANY, msg)
wx.PostEvent(self.parent, event)
msg = ''
break
msg += char
self.serial.close()
def join (self, timeout=None):
self.stopRequest.set()
super(PumpThread, self).join(timeout)
def SetPort (self, serial):
self.serial = serial
def Write (self, msg):
if self.serial.is_open:
self.queue.put(msg)
else:
print("[ERROR]\tPort {} is not open!".format(self.port))
def Stop(self):
if self.isAlive():
self.join()
The Queue is used for sending messages to the port and main loop takes responses back. I've used no serial.readline() method, because of different end-line char, and I have found the usage of io classes to be too much fuss.
Depends on what you run in that thread.
If that's your code, then you can implement a stop condition (see other answers).
However, if what you want is to run someone else's code, then you should fork and start a process. Like this:
import multiprocessing
proc = multiprocessing.Process(target=your_proc_function, args=())
proc.start()
now, whenever you want to stop that process, send it a SIGTERM like this:
proc.terminate()
proc.join()
And it's not slow: fractions of a second.
Enjoy :)
My solution is:
import threading, time
def a():
t = threading.currentThread()
while getattr(t, "do_run", True):
print('Do something')
time.sleep(1)
def getThreadByName(name):
threads = threading.enumerate() #Threads list
for thread in threads:
if thread.name == name:
return thread
threading.Thread(target=a, name='228').start() #Init thread
t = getThreadByName('228') #Get thread by name
time.sleep(5)
t.do_run = False #Signal to stop thread
t.join()
I find it useful to have a class, derived from threading.Thread, to encapsulate my thread functionality. You simply provide your own main loop in an overridden version of run() in this class. Calling start() arranges for the object’s run() method to be invoked in a separate thread.
Inside the main loop, periodically check whether a threading.Event has been set. Such an event is thread-safe.
Inside this class, you have your own join() method that sets the stop event object before calling the join() method of the base class. It can optionally take a time value to pass to the base class's join() method to ensure your thread is terminated in a short amount of time.
import threading
import time
class MyThread(threading.Thread):
def __init__(self, sleep_time=0.1):
self._stop_event = threading.Event()
self._sleep_time = sleep_time
"""call base class constructor"""
super().__init__()
def run(self):
"""main control loop"""
while not self._stop_event.isSet():
#do work
print("hi")
self._stop_event.wait(self._sleep_time)
def join(self, timeout=None):
"""set stop event and join within a given time period"""
self._stop_event.set()
super().join(timeout)
if __name__ == "__main__":
t = MyThread()
t.start()
time.sleep(5)
t.join(1) #wait 1s max
Having a small sleep inside the main loop before checking the threading.Event is less CPU intensive than looping continuously. You can have a default sleep time (e.g. 0.1s), but you can also pass the value in the constructor.
Sometimes you don't have control over the running target. In those cases you can use signal.pthread_kill to send a stop signal.
from signal import pthread_kill, SIGTSTP
from threading import Thread
from itertools import count
from time import sleep
def target():
for num in count():
print(num)
sleep(1)
thread = Thread(target=target)
thread.start()
sleep(5)
pthread_kill(thread.ident, SIGTSTP)
result
0
1
2
3
4
[14]+ Stopped

Categories

Resources