how to execute different system commands in differrent threads with python3 - multithreading

I want to execute two different commands in different thread with python3,below is my code:
import time
import threading
import os
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result():
return self.result
def sniffdata1():
while 1:
time.sleep(1)
os.system("echo 2")
def sniffdata2():
time.sleep(1)
os.system("echo 1")
sniffThread1=MyThread(sniffdata1,())
sniffThread2=MyThread(sniffdata2,())
sniffThread1.start()
sniffThread2.start()
sniffThread1.join()
sniffThread2.join()
But I can not get my 121212...,the result is 1 2 2 2 2 2 2 2 ...(no more 1),but alway echo 2,can someone help me?

If you want get more 1, you should add while to sniffdata2():
def sniffdata2():
while 1:
time.sleep(1)
os.system("echo 1")
But if you want to get 12121212... all the times, you should use this code:
import time
import threading
import os
flag2 = False;
flag1 = True;
class MyThread(threading.Thread):
def __init__(self, func, args, name=''):
threading.Thread.__init__(self)
self.name = name
self.func = func
self.args = args
def run(self):
self.result = self.func(*self.args)
def get_result(self):
return self.result
def sniffdata1():
global flag2, flag1
while 1:
if flag1:
flag2 = False
time.sleep(1)
os.system("echo 1")
flag1 = False
flag2 = True
def sniffdata2():
global flag2, flag1
while 1:
if flag2:
flag1 = False
time.sleep(1)
os.system("echo 2")
flag2 = False
flag1 =True
sniffThread1=MyThread(sniffdata1,())
sniffThread2=MyThread(sniffdata2,())
sniffThread1.start()
sniffThread2.start()

When I use multi process to deal with it,it works,but I am not very clear about the reason,below is my solution code:
from multiprocessing import Process
import os
import time
def worker1():
"""test python multi process"""
while 1:
time.sleep(1)
os.system("echo 1")
#os.system("ping www.baidu.com")
def worker2():
"""test python multi process"""
while 1:
time.sleep(2)
os.system("echo 2")
#os.system("ping www.bing.com")
def main():
jobs = []
p1 = Process(target=worker1,args=())
p2 = Process(target=worker2,args=())
jobs.append(p1)
jobs.append(p2)
p1.start()
p2.start()
##to avoid defunct process,you should call join()
for j in jobs:
j.join()
if __name__=='__main__':
main()

Related

DelphiVCL for Python threading

Using DelphiVCL for Python it seems to be blocking threads.
Testing the code (below) I would expect it to output / update timing from the two - 2 - threads. However the VCL app seems to be blocking the other threads.
How can this be fixed to allow running background threads with VCL?
from delphivcl import *
import threading
from time import sleep, strftime as now
from datetime import datetime
FORMAT = '%Y-%m-%d %H:%M:%S'
class Thread(threading.Thread):
def __init__(self, parent, threadname):
super(Thread, self).__init__()
self._stop = threading.Event()
self.parent = parent
self.name = threadname
def run(self):
while not self._stop.is_set():
text = "{0} {1} {2}".format(now(FORMAT), self.name, str(self._stop.is_set()))
if self.parent:
self.parent.lblHello.Caption = text
else:
print(text, flush=True)
sleep(1)
print(now(FORMAT), self.name, str(self._stop.is_set()))
def stop(self):
self._stop.set()
class GUIApp(Form):
def __init__(self, owner):
self.OnDestroy = self.__form_ondestroy
self.lblHello = Label(self)
self.lblHello.SetProps(Parent=self)
self.thread = Thread(self, 'inside app')
self.thread.start()
self.lblTimer = Label(self)
self.lblTimer.SetProps(Parent=self)
self.lblTimer.Top = 16
self.timer = Timer(self)
self.timer.Interval = 1
self.timer.OnTimer = self.__form_timer
self.timer.Enabled = True
def __form_timer(self, sender):
self.lblTimer.Caption = datetime.utcnow().strftime(FORMAT + '.%f')[:-3]
def __form_ondestroy(self, sender):
self.thread.stop()
if __name__ == '__main__':
Application.Initialize()
Application.Title = "Hello Delphi VCL"
app = GUIApp(Application)
app.Show()
t2 = Thread(None, 'outside app')
t2.start()
FreeConsole()
Application.Run()
app.Destroy()
t2.stop()
EDIT: Adding a timer (code updated) to the VCL app starts the background threads.

PyQt5 unable to stop/kill/exit from QThread

Borrowing code from : Progress Bar Does not Render Until Job is Complete , I tried to to find way to quit/kill a Qthread while it is working, here my code, you can quit the main window while progress bar is working stopping files to be copied:
import os
import sys
import shutil
from PyQt5 import QtCore, QtWidgets
class myProgressDialog(QtWidgets.QProgressDialog):
def __init__(self, parent=None):
super(myProgressDialog, self).__init__(parent=parent)
def closeEvent(self, event):
"""Get the name of active window about to close
"""
print('cant close')
event.ignore()
class MainWindow(QtWidgets.QMainWindow):
startMoveFilesSignal = QtCore.pyqtSignal(str, str)
def __init__(self):
super(MainWindow, self).__init__()
# srcdir = "/media/zachlab/Windows/LinuxStorage/old/embryos"
# dstdir = "/media/zachlab/Windows/LinuxStorage/old/out"
srcdir = "in"
dstdir = "out"
self.le_src = QtWidgets.QLineEdit(srcdir)
self.le_dst = QtWidgets.QLineEdit(dstdir)
self.button = QtWidgets.QPushButton("Copy")
# self.button.clicked.connect(self.archiveEntry)
self.button.clicked.connect(self.archiveEntry2)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QFormLayout(central_widget)
lay.addRow("From: ", self.le_src)
lay.addRow("To: ", self.le_dst)
lay.addRow(self.button)
print('self,thread :', self.thread)
def archiveEntry2(self):
print('connected')
self.progressbar = myProgressDialog(self)
# RIMUOVO Cancel Button
self.progressbar.setCancelButton(None)
self.progressbar.hide()
self.thread = QtCore.QThread(self)
self.thread.start()
self.helper = MoveFileHelper()
self.startMoveFilesSignal.connect(self.helper.moveFilesWithProgress)
self.helper.progressChanged.connect(self.progressbar.setValue)
self.helper.finished.connect(self.on_finished)
self.helper.started.connect(self.progressbar.show)
self.helper.errorOccurred.connect(self.on_errorOcurred)
self.helper.moveToThread(self.thread)
self.archiveEntry()
## Questo funziona
def closeEvent(self, event):
"""Get the name of active window about to close
"""
print('killing thread')
try:
if self.thread.isRunning():
print('killing running thread', self.thread.isRunning())
# self.thread.terminate() ## ---------> error Qt has caught an exception thrown from an event handler.
self.thread.quit() ### funziona ma non in SPYDER
except Exception as Exceptionz:
print('Exception :', Exceptionz)
try:
print('killing running thread after quit :', self.thread.isRunning())
except:
print('quitted')
event.accept()
#QtCore.pyqtSlot()
def archiveEntry(self):
self.startMoveFilesSignal.emit(self.le_src.text(), self.le_dst.text())
self.progressbar.hide()
#QtCore.pyqtSlot()
def on_finished(self):
self.button.setText('Finished')
#QtCore.pyqtSlot(str)
def on_errorOcurred(self, msg):
QtWidgets.QMessageBox.critical(self, "Error Ocurred", msg)
class MoveFileHelper(QtCore.QObject):
progressChanged = QtCore.pyqtSignal(int)
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
errorOccurred = QtCore.pyqtSignal(str)
def calculateAndUpdate(self, done, total):
progress = int(round((done / float(total)) * 100))
self.progressChanged.emit(progress)
#staticmethod
def countFiles(directory):
count = 0
if os.path.isdir(directory):
for path, dirs, filenames in os.walk(directory):
count += len(filenames)
return count
#staticmethod
def makedirs(dest):
if not os.path.exists(dest):
os.makedirs(dest)
#QtCore.pyqtSlot(str, str)
def moveFilesWithProgress(self, src, dest):
numFiles = MoveFileHelper.countFiles(src)
# if os.path.exists(dest):
# self.errorOccurred.emit("Dest exist")
# return
if numFiles > 0:
self.started.emit()
MoveFileHelper.makedirs(dest)
numCopied = 0
for path, dirs, filenames in os.walk(src):
for directory in dirs:
destDir = path.replace(src, dest)
MoveFileHelper.makedirs(os.path.join(destDir, directory))
for sfile in filenames:
srcFile = os.path.join(path, sfile)
destFile = os.path.join(path.replace(src, dest), sfile)
shutil.copy(srcFile, destFile)
numCopied += 1
self.calculateAndUpdate(numCopied, numFiles)
for i in range(100000):
i = i*10
self.finished.emit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainWindow()
ex.resize(640, ex.sizeHint().height())
ex.show()
sys.exit(app.exec_())
Not sure if it is the right way to kill a Qthread but seems to work.
Even if after stopping the Qthread: self.thread.quit() I stop the copying of files
but the self.thread.isRunning() still returns True.
When trying to split the code and add another window using:
main.py
import os
import sys
import shutil
from PyQt5 import QtCore, QtWidgets
from mod007b_import import Windowz, MoveFileHelper, myProgressDialog
class MainWindow(QtWidgets.QMainWindow):
def __init__(self):
super(MainWindow, self).__init__()
QtWidgets.QMainWindow.__init__(self)
self.layout = QtWidgets.QHBoxLayout()
self.lineEdit = QtWidgets.QLineEdit()
self.lineEdit.setText("Just to fill up the dialog")
self.layout.addWidget(self.lineEdit)
self.button = QtWidgets.QPushButton('pppppp')
self.layout.addWidget(self.button)
self.widget = QtWidgets.QWidget()
self.widget.setLayout(self.layout)
self.setCentralWidget(self.widget)
self.setWindowTitle('Simple')
self.button.clicked.connect(self.newWindow)
self.listz = []
def newWindow(self):
print('newwindow')
self.pippo = Windowz() ########## RIVEDERE PARENT CHILD RELATIONSHIP
self.pippo.show()
# self.listz.append(self.pippo)
pw = self.pippo.parentWidget()
print('list : ', self.listz)
print(pw)
if pw is not None:
print('self :', self)
print('pw : ', pw, pw.layout)
print('pippo :', self.pippo)
# print(' central_widget :', central_widget, type( central_widget))
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainWindow()
# ex.setWindowTitle('Simple**************')
ex.resize(640, ex.sizeHint().height())
ex.show()
sys.exit(app.exec_())
and
mod007b_import.py
import os
import sys
import shutil
from PyQt5 import QtCore, QtWidgets
class myProgressDialog(QtWidgets.QProgressDialog):
def __init__(self, parent=None):
super(myProgressDialog, self).__init__(parent=parent)
def closeEvent(self, event):
"""Get the name of active window about to close
"""
print('cant close')
event.ignore()
class Windowz(QtWidgets.QWidget):
# class Windowz(QtWidgets.QMainWindow):
startMoveFilesSignal = QtCore.pyqtSignal(str, str)
# def __init__(self,parent=None):
# # super(Windowz, self).__init__(parent=parent)
# super(Windowz, self).__init__(parent=parent)
def __init__(self):
# super(Windowz, self).__init__(parent=parent)
super().__init__()
# srcdir = "/media/zachlab/Windows/LinuxStorage/old/embryos"
# dstdir = "/media/zachlab/Windows/LinuxStorage/old/out"
srcdir = "in"
dstdir = "out"
self.le_src = QtWidgets.QLineEdit(srcdir)
self.le_dst = QtWidgets.QLineEdit(dstdir)
self.button = QtWidgets.QPushButton("Copy")
# self.button.clicked.connect(self.archiveEntry)
self.button.clicked.connect(self.archiveEntry2)
### spostati in Main
# central_widget2 = QtWidgets.QWidget()
# self.setCentralWidget(central_widget2)
# lay = QtWidgets.QFormLayout(central_widget2)
self.lay = QtWidgets.QFormLayout(self)
self.lay.addRow("From: ", self.le_src)
self.lay.addRow("To: ", self.le_dst)
self.lay.addRow(self.button)
print('self,thread :', self.thread)
# self.show()
def archiveEntry2(self):
print('connected')
self.progressbar = myProgressDialog(self)
# RIMUOVO Cancel Button
self.progressbar.setCancelButton(None)
self.progressbar.hide()
self.thread = QtCore.QThread(self)
self.thread.start()
self.helper = MoveFileHelper()
self.startMoveFilesSignal.connect(self.helper.moveFilesWithProgress)
self.helper.progressChanged.connect(self.progressbar.setValue)
self.helper.finished.connect(self.on_finished)
self.helper.started.connect(self.progressbar.show)
self.helper.errorOccurred.connect(self.on_errorOcurred)
self.helper.moveToThread(self.thread)
self.archiveEntry()
## Questo funziona
def closeEvent(self, event):
"""Get the name of active window about to close
"""
print('killing thread')
try:
if self.thread.isRunning():
print('killing running thread', self.thread.isRunning())
# self.thread.terminate() ## ---------> error Qt has caught an exception thrown from an event handler.
self.thread.quit() ### doesnt work
# self.progressbar.hide() ### hides the bar
# self.progressbar.close() ### doesnt work
try:
print('killing running thread after quit :', self.thread.isRunning())
except:
print('quitted')
except Exception as Exceptionz:
print('Exception :', Exceptionz)
event.accept()
#QtCore.pyqtSlot()
def archiveEntry(self):
self.startMoveFilesSignal.emit(self.le_src.text(), self.le_dst.text())
self.progressbar.hide()
#QtCore.pyqtSlot()
def on_finished(self):
self.button.setText('Finished')
#QtCore.pyqtSlot(str)
def on_errorOcurred(self, msg):
QtWidgets.QMessageBox.critical(self, "Error Ocurred", msg)
class MoveFileHelper(QtCore.QObject):
progressChanged = QtCore.pyqtSignal(int)
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
errorOccurred = QtCore.pyqtSignal(str)
def calculateAndUpdate(self, done, total):
progress = int(round((done / float(total)) * 100))
self.progressChanged.emit(progress)
#staticmethod
def countFiles(directory):
count = 0
if os.path.isdir(directory):
for path, dirs, filenames in os.walk(directory):
count += len(filenames)
return count
#staticmethod
def makedirs(dest):
if not os.path.exists(dest):
os.makedirs(dest)
#QtCore.pyqtSlot(str, str)
def moveFilesWithProgress(self, src, dest):
numFiles = MoveFileHelper.countFiles(src)
# if os.path.exists(dest):
# self.errorOccurred.emit("Dest exist")
# return
if numFiles > 0:
self.started.emit()
MoveFileHelper.makedirs(dest)
numCopied = 0
for path, dirs, filenames in os.walk(src):
for directory in dirs:
destDir = path.replace(src, dest)
MoveFileHelper.makedirs(os.path.join(destDir, directory))
for sfile in filenames:
srcFile = os.path.join(path, sfile)
destFile = os.path.join(path.replace(src, dest), sfile)
shutil.copy(srcFile, destFile)
numCopied += 1
self.calculateAndUpdate(numCopied, numFiles)
for i in range(100000):
i = i*10
self.finished.emit()
I get a first window, pressing the 'pppppp' button it goes to a second one that is the same as the single file script above: press 'copy' button to start the copying/Qthread, but when I close this window even if the QThread seems to be stopped, progress bar doesnt disappear, I can hide the progress bar but cant close it and in any case the copying process reach completion.
Any idea what is going on ?
PS
in order to have the script working and a having a visible progress bar files need to be in a directory toghether with a 'in' folder with enough files to have a slow process.
OK thanks to #musicamante and to Stopping an infinite loop in a worker thread in PyQt5 the simplest way
I figured out what was wrong in the first of my two codes, here the first one re-written with a flag set to terminate the copying loop when main window is closed, important
commenting out or not :
# self.thread.quit()
# self.thread.wait()
in Mainwindow def closeEvent(self, event): just after the setting of the flag to True (self.ctrl['break'] = True) will end up having the QThread running / not running before the script terminates anyway. For the flag see the Solution 2: Passing a mutable as a control variable in Stopping an infinite loop in a worker thread in PyQt5 the simplest way
import os
import sys
import shutil
from PyQt5 import QtCore, QtWidgets
class myProgressDialog(QtWidgets.QProgressDialog):
def __init__(self, parent=None):
super(myProgressDialog, self).__init__(parent=parent)
def closeEvent(self, event):
"""Get the name of active window about to close
"""
print('cant close')
event.ignore()
class MainWindow(QtWidgets.QMainWindow):
startMoveFilesSignal = QtCore.pyqtSignal(str, str)
def __init__(self):
super(MainWindow, self).__init__()
# srcdir = "/media/zachlab/Windows/LinuxStorage/old/embryos"
# dstdir = "/media/zachlab/Windows/LinuxStorage/old/out"
srcdir = "in"
dstdir = "out"
self.le_src = QtWidgets.QLineEdit(srcdir)
self.le_dst = QtWidgets.QLineEdit(dstdir)
self.button = QtWidgets.QPushButton("Copy")
# self.button.clicked.connect(self.archiveEntry)
self.button.clicked.connect(self.archiveEntry2)
central_widget = QtWidgets.QWidget()
self.setCentralWidget(central_widget)
lay = QtWidgets.QFormLayout(central_widget)
lay.addRow("From: ", self.le_src)
lay.addRow("To: ", self.le_dst)
lay.addRow(self.button)
self.ctrl = {'break': False} # dict with your control variable
print('id of ctrl in MainWindow:', id(self.ctrl))
def archiveEntry2(self):
print('connected')
self.progressbar = myProgressDialog(self)
self.progressbar.setWindowTitle('coopying files')
# RIMUOVO Cancel Button
self.progressbar.setCancelButton(None)
self.progressbar.hide()
self.thread = QtCore.QThread(self)
self.thread.start()
self.helper = MoveFileHelper(self.ctrl)
self.startMoveFilesSignal.connect(self.helper.moveFilesWithProgress)
self.helper.progressChanged.connect(self.progressbar.setValue)
self.helper.finished.connect(self.on_finished)
self.helper.started.connect(self.progressbar.show)
self.helper.errorOccurred.connect(self.on_errorOcurred)
self.helper.moveToThread(self.thread)
self.archiveEntry()
## Questo funziona
def closeEvent(self, event):
"""Get the name of active window about to close
"""
try:
print('killing thread')
print('self.thread.isRunning() before quit :', self.thread.isRunning())
if self.thread.isRunning():
print("quitted ----> self.ctrl['break'] = True")
self.ctrl['break'] = True
self.thread.quit()
self.thread.wait()
except Exception as Exceptionz:
print('Exception :', Exceptionz)
try:
print('self.thread.isRunning() after quit :', self.thread.isRunning())
except:
print('quitted')
# event.accept() # not needed implicit
# event.ignore()
#QtCore.pyqtSlot()
def archiveEntry(self):
self.startMoveFilesSignal.emit(self.le_src.text(), self.le_dst.text())
self.progressbar.hide()
#QtCore.pyqtSlot()
def on_finished(self):
print('on_finished self.ctrl inside worker : ', self.ctrl)
print('self.thread.isRunning() after quit on_finished :', self.thread.isRunning())
self.button.setText('Finished')
#QtCore.pyqtSlot(str)
def on_errorOcurred(self, msg):
QtWidgets.QMessageBox.critical(self, "Error Ocurred", msg)
class MoveFileHelper(QtCore.QObject):
progressChanged = QtCore.pyqtSignal(int)
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
errorOccurred = QtCore.pyqtSignal(str)
def __init__(self, ctrl):
super().__init__()
self.ctrl = ctrl # dict with your control var
print('self.ctrl inside worker MoveFileHelper : ', self.ctrl)
print('Entered run in worker thread')
print('id of ctrl in worker:', id(self.ctrl))
self.ctrl['break'] = False
def calculateAndUpdate(self, done, total):
progress = int(round((done / float(total)) * 100))
self.progressChanged.emit(progress)
#staticmethod
def countFiles(directory):
count = 0
if os.path.isdir(directory):
for path, dirs, filenames in os.walk(directory):
count += len(filenames)
return count
#staticmethod
def makedirs(dest):
if not os.path.exists(dest):
os.makedirs(dest)
#QtCore.pyqtSlot(str, str)
def moveFilesWithProgress(self, src, dest):
numFiles = MoveFileHelper.countFiles(src)
# if os.path.exists(dest):
# self.errorOccurred.emit("Dest exist")
# return
if numFiles > 0:
self.started.emit()
MoveFileHelper.makedirs(dest)
numCopied = 0
for path, dirs, filenames in os.walk(src):
for directory in dirs:
destDir = path.replace(src, dest)
MoveFileHelper.makedirs(os.path.join(destDir, directory))
for sfile in filenames:
if self.ctrl['break'] : # == True : is implicit
self.finished.emit()
return
else:
srcFile = os.path.join(path, sfile)
destFile = os.path.join(path.replace(src, dest), sfile)
shutil.copy(srcFile, destFile)
numCopied += 1
self.calculateAndUpdate(numCopied, numFiles)
for i in range(100000):
i = i*10
self.finished.emit()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
ex = MainWindow()
ex.resize(640, ex.sizeHint().height())
ex.show()
sys.exit(app.exec_())

Connecting external threaded log to PyQt5 QPlainTextEdit

I'm new to PyQt and handlers in general, I tried to read from every repo I found but I can't figure out how to fix my issue, as you can see in my code, I'm executing logging thread in the background and I'm trying to show the logs in my QPlainTextEdit console - for some reason I can see the logs in my terminal and the text_box doesn't get the logs at all,
I will appreciate a lot your smart help.
import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
import os
import threading
import json
class ConsolePanelHandler(logging.Handler):
def __init__(self, stream):
# super().__init__()
logging.Handler.__init__(self)
# logging.StreamHandler.__init__(self, stream)
self.stream = stream
def handle(self, record):
rv = self.filter(record)
if rv:
self.acquire()
try:
self.emit(record)
finally:
self.release()
return rv
def emit(self, record):
try:
stream = self.stream
stream(self.format(record))
except RecursionError:
raise
except Exception:
self.handleError(self.format(record))
def thread():
for index in range(20):
logging.warning('scheiBe '+str(index))
time.sleep(1)
class MainWindow(QMainWindow):
def __init__(self):
super().__init__()
layout = QVBoxLayout()
self.continue_ = QPushButton("Continue")
self.continue_.setStyleSheet("background-color: green")
self.continue_.setFont(QFont('SansSerif', 10))
self.continue_.setFixedSize(QSize(300, 22))
self.pause = QPushButton("Pause")
self.pause.setStyleSheet("background-color: orange")
self.pause.setFont(QFont('SansSerif', 10))
self.pause.setFixedSize(QSize(300, 22))
self.stop = QPushButton("Stop")
self.stop.setStyleSheet("background-color: #FD4B4B")
self.stop.setFont(QFont('SansSerif', 10))
self.stop.setFixedSize(QSize(300, 22))
self.text_box = QPlainTextEdit()
self.text_box.setPlaceholderText("Bugs will be printed here")
self.text_box.setReadOnly(True)
logging.getLogger().addHandler(self.text_box)
logging.getLogger().setLevel(logging.DEBUG)
ConsolePanelHandler(self.appendDebug , logging.DEBUG)
self.text_box.moveCursor(QTextCursor.End)
layout.addWidget(self.continue_)
layout.addWidget(self.pause)
layout.addWidget(self.stop)
layout.addWidget(self.text_box)
self.w = QWidget()
self.w.setLayout(layout)
self.setCentralWidget(self.w)
thread1 = threading.Thread(target=thread, args=(), daemon=True)
thread1.start()
self.show()
def closeEvent(self, event):
close = QMessageBox()
close.setText("Are you sure want to stop and exit?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
sys.exit()
else:
event.ignore()
def appendDebug(self, string):
self.text_box.appendPlainText(string +'\n')
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
sys.exit(app.exec())
First, you should not pass the function that updates the text_area instead you should create a pyqtSignal which updates the text_area and pass it to ConsolePanelHandler.
Add ConsolePanelHandler as handler not text_area to logging.getLogger().addHandler()
and it is recommended to use QThread instead of threading.Thread
here is the complete updated code.
# import pandas as pd
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
import time
import os
import sys
import threading
import json
import logging
class ConsolePanelHandler(logging.Handler):
def __init__(self, sig):
# super().__init__()
logging.Handler.__init__(self)
# logging.StreamHandler.__init__(self, stream)
self.stream = sig
def handle(self, record):
rv = self.filter(record)
if rv:
self.acquire()
try:
self.emit(record)
finally:
self.release()
return rv
def emit(self, record):
try:
self.stream.emit(self.format(record))
except RecursionError:
raise
except Exception:
self.handleError(record)
class thread(QThread):
def run(self) -> None:
for index in range(20):
logging.warning('scheiBe ' + str(index))
self.sleep(1)
class MainWindow(QMainWindow):
sig = pyqtSignal(str)
def __init__(self):
super().__init__()
self.layout = QVBoxLayout()
self.continue_ = QPushButton("Continue")
self.continue_.setStyleSheet("background-color: green")
self.continue_.setFont(QFont('SansSerif', 10))
self.continue_.setFixedSize(QSize(300, 22))
self.pause = QPushButton("Pause")
self.pause.setStyleSheet("background-color: orange")
self.pause.setFont(QFont('SansSerif', 10))
self.pause.setFixedSize(QSize(300, 22))
self.stop = QPushButton("Stop")
self.stop.setStyleSheet("background-color: #FD4B4B")
self.stop.setFont(QFont('SansSerif', 10))
self.stop.setFixedSize(QSize(300, 22))
self.c = ConsolePanelHandler(self.sig)
self.text_box = QPlainTextEdit()
self.text_box.setPlaceholderText("Bugs will be printed here")
self.text_box.setReadOnly(True)
logging.getLogger().addHandler(self.c)
logging.getLogger().setLevel(logging.DEBUG)
self.sig.connect(self.appendDebug)
self.text_box.moveCursor(QTextCursor.End)
self.layout.addWidget(self.continue_)
self.layout.addWidget(self.pause)
self.layout.addWidget(self.stop)
self.layout.addWidget(self.text_box)
self.w = QWidget()
self.w.setLayout(self.layout)
self.setCentralWidget(self.w)
self.thread1 = thread(self) # self is parent for Qthread so Qthread will be destroyed when it's parent no longer exist
self.thread1.start()
self.show()
def closeEvent(self, event):
close = QMessageBox()
close.setText("Are you sure want to stop and exit?")
close.setStandardButtons(QMessageBox.Yes | QMessageBox.Cancel)
close = close.exec()
if close == QMessageBox.Yes:
self.thread1.terminate()
sys.exit()
else:
event.ignore()
#pyqtSlot(str)
def appendDebug(self, string):
self.text_box.appendPlainText(string + '\n')
if __name__ == "__main__":
app = QApplication([])
window = MainWindow()
sys.exit(app.exec())

Pyqt5 stop QThread worker on QAction

hello I have QTheard worker that start with click at QAction.
....
self.start_update = QAction('&Start', self)
self.start_update.triggered.connect(self._start_thread)
self.stop_update.setVisible(True)
self.stop_update.triggered.connect(self._stop_thread)
self.stop_update.setVisible(False)
...
...
def _start_thread(self):
self.start_update.setVisible(False)
self.stop_update.setVisible(True)
self.myworker.start()
...
...
def _stop_thread(self):
self.myworker.stop() # That close app with error
self.stop_update.setVisible(False)
self.start_update.setVisible(True)
...
Please help how to close terminate Worker correctly. Atm worker have __del__ and run (logic) method. Maybe need to add some method for correct worker closing?
Your application might look like this:
import sys
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtWidgets import *
class AThread(QThread):
threadSignalAThread = pyqtSignal(int)
def __init__(self):
super().__init__()
def run(self):
count = 0
while count < 1000:
QThread.msleep(200)
count += 1
self.threadSignalAThread.emit(count)
class MsgBoxAThread(QDialog):
def __init__(self):
super().__init__()
layout = QVBoxLayout(self)
self.label = QLabel("")
layout.addWidget(self.label)
close_btn = QPushButton("Close window")
layout.addWidget(close_btn)
close_btn.clicked.connect(self.close)
self.setGeometry(900, 300, 400, 80)
self.setWindowTitle('MsgBox AThread(QThread)')
class Example(QMainWindow):
def __init__(self, parent=None, *args):
super().__init__(parent, *args)
self.setWindowTitle("Pyqt5 stop QThread worker on QAction")
self.setGeometry(550, 300, 300, 300)
centralWidget = QWidget(self)
self.setCentralWidget(centralWidget)
layout = QVBoxLayout(centralWidget)
self.lbl = QLabel("Start")
layout.addWidget(self.lbl)
bar = self.menuBar()
barThread = bar.addMenu('Thread')
quit = bar.addMenu('Quit')
quit.aboutToShow.connect(app.quit)
self.start_update = QAction('&Start', self)
self.start_update.setShortcut('Ctrl+S')
self.start_update.triggered.connect(self._start_thread)
self.stop_update = QAction('Sto&p', self)
self.stop_update.setShortcut('Ctrl+P')
self.stop_update.setVisible(False)
self.stop_update.triggered.connect(self._stop_thread)
barThread.addAction(self.start_update)
barThread.addAction(self.stop_update)
self.msg = MsgBoxAThread()
self.myworker = None
self.counter = 0
self.timer = QTimer()
self.timer.setInterval(1000)
self.timer.timeout.connect(self.recurring_timer)
self.timer.start()
self.show()
def recurring_timer(self):
self.counter += 1
self.lbl.setText(" Do something in the GUI: <b> %d </b>" % self.counter)
def _start_thread(self):
self.start_update.setVisible(False)
self.stop_update.setVisible(True)
self.myworker = AThread()
self.myworker.threadSignalAThread.connect(self.on_threadSignalAThread)
self.myworker.finished.connect(self.finishedAThread)
self.myworker.start()
def finishedAThread(self):
self.myworker = None
self.start_update.setVisible(True)
self.stop_update.setVisible(False)
def on_threadSignalAThread(self, value):
self.msg.label.setText(str(value))
if not self.msg.isVisible():
self.msg.show()
def _stop_thread(self):
self.stop_update.setVisible(False)
self.start_update.setVisible(True)
self.myworker.terminate()
self.myworker = None
def closeEvent(self, event):
reply = QMessageBox.question(self, 'Question',
"Are you sure you want to close the application?",
QMessageBox.Yes,
QMessageBox.No)
if reply == QMessageBox.Yes:
if self.myworker:
self.myworker.quit()
del self.myworker
self.msg.close()
super(Example, self).closeEvent(event)
else:
event.ignore()
if __name__ == '__main__':
app = QApplication(sys.argv)
ex = Example()
#ex.show()
sys.exit(app.exec_())

how to interrupt a thread

i want to kill a thread "mid run" i have 2 loops one dose some things (lest call it 1) while the other (lets call it 2) listens to a keyboard event and i want 1 to terminate as soon as 2 detects the event
i am sorry that my code is a mess it can probably be ignored there is not to much i was able to do
import threading
import keyboard
import time
r = sr.Recognizer()
class Main(threading.Thread):
def __init__(self):
super(Main, self).__init__()
self._stop_event = threading.Event()
def stop(self):
self._stop_event.set()
def stopped(self):
return self._stop_event.is_set()
def run(self):
main()
class Listener(threading.Thread):
def run(self):
listener()
def listener():
time.sleep(0.3)
keyboard.wait('alt + s')
print('stop')
_main.stop()
time.sleep(0.3)
keyboard.wait('alt + s')
print('start')
_main.start()
def main():
_listener.start()
while 1:
print('m')
time.sleep(1)
if __name__ == '__main__':
_main = Main()
_listener = Listener()
keyboard.wait('alt + s')
_main.start()
keyboard.wait('esc')

Resources