I am trying to loop an alarm (in the file beep.wav) and show an alert.
Once the alert is closed, I want to instantly stop the alarm.
I am attempting a solution which uses threading to control the alarm's playback.
However, it throws an error:
Traceback (most recent call last):
File "test.py", line 28, in <module>
thread.stop()
File "test.py", line 21, in stop
self.process.kill()
AttributeError: 'AlarmThread' object has no attribute 'process'
I don't really know why this error would get thrown, but it looks like self.process is, for some reason, not assigned when AlarmThread.stop is called.
This makes no sense to me, as from my code it looks like thread.stop is only called after thread.start:
import subprocess
import threading
class AlarmThread(threading.Thread):
def __init__(self, file_name="beep.wav"):
super(AlarmThread, self).__init__()
self.file_name = file_name
self.ongoing = None
def run(self):
self.ongoing = True
while self.ongoing:
self.process = subprocess.Popen(["afplay", self.file_name])
self.process.wait()
def stop(self):
if self.ongoing is not None:
self.ongoing = False
self.process.kill()
thread = AlarmThread()
thread.start()
# show_alert is synchronous, an alert must be closed before the script continues
show_alert("1 second timer")
thread.stop()
thread.join()
You have a race condition. The thread hasn't had time to start, create the process and assign self.process by the time you call thread.stop(). You could initialize self.process in __init__ and use that to see if the process is really there
import subprocess
import threading
class AlarmThread(threading.Thread):
def __init__(self, file_name="beep.wav"):
super(AlarmThread, self).__init__()
self.lock = threading.Lock()
self.file_name = file_name
self.ongoing = False
self.process = None
def run(self):
self.ongoing = True
while True:
with self.lock:
if not self.ongoing:
break
self.process = subprocess.Popen(["afplayer", self.file_name])
self.process.wait()
def stop(self):
with self.lock:
if self.ongoing:
self.ongoing = False
if self.process:
self.process.kill()
thread = AlarmThread()
thread.start()
# show_alert is synchronous, an alert must be closed before the script continues
show_alert("1 second timer")
thread.stop()
thread.join()
Yes, it was caused by a race condition.
The thread hasn't had time to start, create the process and assign self.process by the time you call thread.stop()
However, I found a fix that relied upon simply waiting until thread.process was assigned:
thread = AlarmThread()
thread.start()
while not thread.process:
time.sleep(0.1)
show_alert(message)
thread.stop()
thread.join()
My class also changed slightly to ensure thread.process is always assigned:
class AlarmThread(threading.Thread):
def __init__(self, file_name="beep.wav"):
super(AlarmThread, self).__init__()
self.file_name = file_name
self.ongoing = None
self.process = None
def run(self):
self.ongoing = True
while self.ongoing:
self.process = subprocess.Popen(["afplay", self.file_name])
self.process.wait()
self.process = None
def stop(self):
if self.ongoing is not None:
self.ongoing = False
self.process.kill()
Related
So basically I created a script called worker which contain 2 classes:
Worker
CreateThread
this script is supposed to handle unexpected exception occurs, while also provide threading solution to running heavy function to prevent GUI freezing.
My problem is that I get unexpected behavior sometimes, for example one time I can run function with the worker class and I will get a good response and everything works fine, and second time things go wrong and I get odd errors and also GUI crashes.
I tried to change the order I manage the thread handling, and I tried addepting the code to the errors but there is a hidden problem that keeps hunting me...
that is my worker code:
# Create a worker class #
class Worker(QObject):
my_started = pyqtSignal()
my_finished = pyqtSignal()
my_error = pyqtSignal(str, str)
my_success = pyqtSignal()
def __init__(self):
super(Worker, self).__init__()
def run(self, func):
try:
self.my_started.emit()
time.sleep(0.1)
x = func()
if x is None:
self.my_success.emit()
self.my_finished.emit()
return
if x[0] == -1:
function_error = x[1]
trace_back = x[2]
print(f'\nFunction error: {function_error}')
print(trace_back)
self.my_error.emit(str(function_error), trace_back,
str(func.__name__))
self.my_finished.emit()
return
except Exception as e:
self.trace_back = str(traceback.format_exc())
print(f'\nFunction error: {e}')
print(self.trace_back)
self.my_error.emit(str(e), self.trace_back)
self.my_finished.emit()
# Creating thread manger class #
class CreateThread(Worker, QObject):
def __init__(self, atp_name):
super(CreateThread, self).__init__()
self.stop_flag = False
self.atp_name = atp_name
self.thread = QThread()
self.worker = Worker()
def start_thread(self, func, **kwargs):
self.kwargs = kwargs
self.func_name = func.__name__
if self.stop_flag:
return
if self.func_name == '<lambda>':
self.func_name = self.kwargs['func_name']
# Thread and Worker init #
self.thread = QThread()
self.worker = Worker()
act = partial(self.worker.run, func)
self.worker.moveToThread(self.thread)
self.thread.started.connect(act)
# Signal to Function connection #
self.worker.my_started.connect(self.started_signal_connection)
self.worker.my_finished.connect(self.finished_signal_connection)
self.worker.my_error.connect(self.error_signal_connection)
self.worker.my_success.connect(self.success_signal_connection)
# Handling the worker and the thread deletion #
self.worker.my_finished.connect(self.thread.quit)
self.worker.my_finished.connect(self.worker.deleteLater)
self.thread.finished.connect(self.thread.deleteLater)
self.thread.start()
# Connect signals from worker #
def started_signal_connection(self):
self.stop_flag = True
print(f"\nFunction {self.func_name} started\n")
def finished_signal_connection(self):
self.stop_flag = False
print("\nWorker finished\n")
def error_signal_connection(self, error_name, trace_back):
self.today = date.today()
self.now = datetime.now()
self.current_date = str(
self.now.strftime("%d/%m/%Y %H:%M:%S")).replace(' ',
'_').replace(
'/', '_').replace(':', '_')
self.header_name = f'{self.atp_name}_{self.func_name}_Date_{self.current_date}'
self.target_folder = 'target_folder'
if os.path.isfile(self.header_name) == False:
file_name = f'{self.target_folder}{self.header_name}.txt'
try:
with open(file_name, 'w', encoding='utf-8') as f:
f.write(f'''ATP: {self.atp_name}\nFunction name: {self.func_name}\nError: {error_name}\nDate: {self.current_date}\n\n{trace_back}''')
except Exception as e:
print(e)
def success_signal_connection(self):
self.stop_flag = False
print("\nFunction success")
Another thing I noticed is that every time I use the worker with function that use Tk - askopenfilename()
I get troubles, here is a traceback example:
line 57, in run
**x = func()**
line 64, in open_main_terminal
**self.path1, self.file_name = upload_item()**
line 62, in upload_item
**filename = askopenfilename()**
line 375, in askopenfilename
**return Open(**options).show()**
line 39, in show
**w = Frame(self.master)**
line 2741, in __init__
**Widget.__init__(self, master, 'frame', cnf, {}, extra)**
line 2296, in __init__ (widgetName, self._w) + extra + self._options(cnf))
**RuntimeError: main thread is not in main loop**
Here is my script. When I run it in a shell it just hangs indefinitely whereas I would expect it to terminate cleanly.
import logging
from logging import StreamHandler
import pymsteams
import queue
import threading
import atexit
class TeamsHandler(StreamHandler):
def __init__(self, channel_url):
super().__init__()
self.channel_url = channel_url
self.queue = queue.Queue()
self.thread = threading.Thread(target=self._worker)
self.thread.start()
atexit.register(self.queue.put, None)
def _worker(self):
while True:
record = self.queue.get()
if record is None:
break
msg = self.format(record)
print(msg)
def emit(self, record):
# enqueue the record to log and return control to the caller
self.queue.put(record)
if __name__ == "__main__":
my_logger = logging.getLogger('TestLogging')
my_logger.setLevel(logging.DEBUG)
console_handler = logging.StreamHandler()
console_handler.setLevel(logging.DEBUG)
my_logger.addHandler(console_handler)
CHANNEL_ID = "not_used_anyway"
teamshandler = TeamsHandler(CHANNEL_ID)
teamshandler.setFormatter(logging.Formatter('%(levelname)s %(message)s'))
teamshandler.setLevel(logging.DEBUG)
my_logger.addHandler(teamshandler)
for i in range(1, 2):
my_logger.error(f"this is an error [{i}]")
my_logger.info(f"this is an info [{i}]")
The None record that should be sent by atexit (line 28) never arrives so the thread stays open forever.
How to make sure that the program exits cleanly by modifying the TeamsHandler only ?
I got something working, have a look:
import queue
import threading
class Worker:
def __init__(self):
self.queue = queue.Queue()
threading.Thread(target=self._worker).start()
def _worker(self):
print("starting thread")
while True:
record = self.queue.get()
if record is None:
print("exiting")
break
print(f"Got message: {record}")
def emit(self, record):
self.queue.put(record)
class Wrapper:
def __init__(self):
self._worker = Worker()
def __del__(self):
print("Wrapper is being deleted")
self._worker.emit(None)
def emit(self, record):
self._worker.emit(record)
def main():
worker = Wrapper()
worker.emit("foo")
worker.emit("bar")
print("main exits")
if __name__ == "__main__":
main()
The point here is that when main exits, worker (which is an instance of Wrapper) goes out of scope, and its __del__ method is called, and it sends stop message to a real worker object.
The results of running this code ("Got message" lines can be in different places, of course):
starting thread
main exits
Wrapper is being deleted
Got message: foo
Got message: bar
exiting
As pointed out by avysk, the problem is likely that atexit handlers fire too late, after the waiting for the non-daemon threads is already (supposed to be) done, which leads to deadlock.
If I were you, I'd just add a call like TeamsHandler.finish() at the end of if __name__ == '__main__' block, and modify TeamsHandler along these lines (untested):
_queues = []
class TeamsHandler(StreamHandler):
def __init__(self, channel_url):
super().__init__()
self.channel_url = channel_url
self.queue = queue.Queue()
self.thread = threading.Thread(target=self._worker)
self.thread.start()
_queues.append(self.queue)
def _worker(self):
while True:
record = self.queue.get()
if record is None:
break
msg = self.format(record)
print(msg)
def emit(self, record):
# enqueue the record to log and return control to the caller
self.queue.put(record)
#staticmethod
def finish(self):
for q in _queues:
q.put(None)
del _queues[:]
i m using PIR sensor to find out intruder is present or not. if intruder is present it will go to sleep for 1 min now i have to reset sleep time, for this i m using thread but it shows assertion error, help me out, below is my code.
from threading import Thread, Event
import time
import RPi.GPIO as GPIO
class MyThread(Thread):
def __ini(self, timeout=60):
self.intruder_spotted = Event()
self.timeout = timeout
self.daemon = True
def run(self):
while True:
if self.intruder_spotted.wait(self.timeout):
self.intruder_spotted.clear()
print("Intruder")
else:
print("No intruder")
t = MyThread(60)
GPIO.setmode(GPIO.BCM)
GPIO.setup(18,GPIO.IN)
try:
t.start()
while True:
i=GPIO.input(18)
if i==1:
t.intruder_spotted.set()
time.sleep(1)
except KeyboardInterrupt:
GPIO.cleanup()
exit(0)
need Update the int of the class my Thread. It's __init__ not __ini. And the call to the parent init with super
class MyThread(Thread):
def__init__(self, timeout=60):
super(MyThread, self).__init__()
self.intruder_spotted = Event()
self.timeout = timeout
self.daemon = True
I'm trying to implement a class for multi-threading and want to use the output to determine what to do next in my program. How can I return the output of self.process as a string? If I try to return the output of self.process.communicate I get an error.
#class for multi threading
class Command(object):
def __init__(self,cmd):
self.cmd = cmd
self.process = None
def run(self,timeout):
def target():
print("Thread started")
self.process = subprocess.Popen(self.cmd,stdout=subprocess.PIPE)
self.process.communicate()
print("Thread finished")
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("\nTerminating process")
self.process.terminate()
thread.join()
print(self.process.returncode)
def unzip_file(zipped):
command = Command(zip_exe+' x '+zipped)
command.run(timeout = 12000)
unzip_file(zipped)
This is usually what I do to get the output of my process:
class SExec:
def __init__(self, _command):
_process = Popen(_command, shell=True, stdout=PIPE, stderr=STDOUT, close_fds=True)
if _process.stderr is None:
self.stdout = (_process.stdout.read()).decode("utf-8")
self.return_code = _process.returncode
else:
self.stdout = None
self.stderr = _process.stderr.decode("utf-8")
Then, when I want, as an example, to execute something and get it's return, I can do:
dir_info = SExec('ls -lA').stdout
for _line in dir_info.split('\n'):
print(_line)
I hope my example helps you. Regards.
I obviously need to come up to speed with OOP. Anyhow here is the answer that I used...
#class for multi threading
class Command(object):
def __init__(self,cmd):
self.cmd = cmd
self.process = None
def run(self,timeout):
def target():
print("Thread started")
self.process = subprocess.Popen(self.cmd,stdout=subprocess.PIPE)
self.tmp = self.process.stdout.read()
self.process.communicate()
print("Thread finished")
thread = threading.Thread(target=target)
thread.start()
thread.join(timeout)
if thread.is_alive():
print("\nTerminating process")
self.process.terminate()
thread.join()
print(self.process.returncode)
Then the results can be accessed by accessing self.tmp after using the instance name: command.tmp.
I would like to make a program which runs two threads which can be simultaneously interrupted using ctrl+C. The following script is a simplified version of this:
import time
import threading
class Controller(object):
def __init__(self, name=None):
self.name = name
def run_once(self):
print("Controller {} is running once...".format(self.name))
def run_forever(self):
while True:
self.run_once()
time.sleep(1)
if __name__ == "__main__":
controller1 = Controller(name="1")
controller2 = Controller(name="2")
thread1 = threading.Thread(target=controller1.run_forever)
thread2 = threading.Thread(target=controller2.run_forever)
thread1.daemon = True
thread2.daemon = True
thread1.start()
thread2.start()
try:
while True:
thread1.join(1)
thread2.join(1)
if not thread1.isAlive() or not thread2.isAlive():
break
except KeyboardInterrupt:
pass
I'm trying to make the code a bit more DRY by doing the following:
import time
import threading
class Controller(object):
def __init__(self, name=None):
self.name = name
def run_once(self):
print("Controller {} is running once...".format(self.name))
def run_forever(self):
while True:
self.run_once()
time.sleep(1)
class ThreadController(Controller, threading.Thread):
def __init__(self, *args, **kwargs):
Controller.__init__(self, *args, **kwargs)
threading.Thread.__init__(self, target=self.run_forever)
self.daemon = True
self.start()
if __name__ == "__main__":
thread1 = ThreadController(name="1")
thread2 = ThreadController(name="2")
try:
while True:
thread1.join(1)
thread2.join(1)
if not thread1.isAlive() or not thread2.isAlive():
break
except KeyboardInterrupt:
pass
However, when I try to run the latter script, I get the following error:
Traceback (most recent call last):
File "threading_test3.py", line 34, in <module>
thread1 = ThreadController(name="1")
File "threading_test3.py", line 18, in __init__
Controller.__init__(self, *args, **kwargs)
File "threading_test3.py", line 6, in __init__
self.name = name
File "/usr/lib/python2.7/threading.py", line 971, in name
assert self.__initialized, "Thread.__init__() not called"
AssertionError: Thread.__init__() not called
I don't understand why Thread.__init__() is not called, because it seems like it is called in the __init__ of ThreadController. What is causing this error?
Call Thread's init, first;
class ThreadController(Controller, threading.Thread):
def __init__(self, *args, **kwargs):
threading.Thread.__init__(self, target=self.run_forever)
Controller.__init__(self, *args, **kwargs)
self.daemon = True
self.start()