0

I have Kali Linux operating system, and lately i have been working with python scripts ( creating GUI using python and QT5) and i am using multiprocessing and multi-threading in my code. However I noticed that lately that whenever I run the script, the whole laptop suffers from latency in responding, even when the script is over, the laptop still doesn't restore its usual speed.

What i have done:

  1. The device is up-to-date.

Simplified Version of the code

from PyQt5 import QtCore, QtGui, QtWidgets
from PyQt5.QtCore import *
from PyQt5.QtWidgets import QApplication, QMainWindow, QMessageBox, QInputDialog
from PyQt5.QtGui import QIcon, QPixmap
from PyQt5.QtCore import QProcess

#loading the GUI..
from GUI  import GUICLASS



class WorkerThread(QThread):
    request_signal = QtCore.pyqtSignal()
    Start_test= QtCore.pyqtSignal(int)
    def __init__(self, parent = None):
        super(WorkerThread, self).__init__(parent)
        print("init")

    @QtCore.pyqtSlot()
    def doWork(self):
        self.request_signal.emit()


    @QtCore.pyqtSlot(int)
    def startTheTest(self):
        self.Start_test.emit("start the test")

    def stop(self):
        self.terminate()
        print("this thread is terminating")


class theMainCode(QMainWindow, Ui_IoTTestbed, reportClass, FuncsToAddDevices):


    def __init__(self):
        super().__init__()
        self.setupUi(self)
        self.initialize_GUI()



    def initialize_GUI(self):
        ####################### Start: Buttons ##############################
        #initialize Buttons
        self.button1.setEnabled(False)
        self.Button2.clicked.connect(self.startAPscript)


    def startAPscript(self, TestingDevice):
        self.startTheThread()




    def startTheThread(self, MAC):

        thread2 = QtCore.QThread(self)
        thread2.start()
        self.worker2 = WorkerThread()
        self.worker2.moveToThread(thread2)
        self.worker2.Start_test.connect(lambda:self.startTesting(0,MAC))
        self.worker2.startTheTest()





    @pyqtSlot(int)
    def startTesting(self,number =0 , MAC="", second=False):
        if not second:
            self.tests=list(TestList.keys())
            print("loading tests: ", self.tests)

        if MAC in self.process and len(self.tests)==0 :
            print("if statment")
            return self.testsDone(MAC)
        print("\n\nThe tests left is:\n ", self.tests)
        time.sleep(2)

        for _ in range(len(self.tests)):
            script_name = self.tests.pop(0)

            self.runTest(script_name)

        if len(self.tests) ==0: #done empty..
            print("Test is done")
            self.testsDone(MAC)



    def testsDone(self, MAC):

        print("kill the threads")
        self.worker2.stop()
        #self.worker.stop()






    #----------------------------------------------------------------------------------------------------------------


    def closeEvent(self, *args, **kwargs):
        super(QMainWindow, self).closeEvent(*args, **kwargs)

        if (self.worker != None):
            print("Killed = 1")
            self.worker.stop()

        if (self.worker2 != None):
            print("Killed = 2")
            self.worker2.stop()
        print("we are done")

        app.exec_()





if __name__ == "__main__":
    app = QApplication(sys.argv)
    main = theMainCode()
    main.show()
    sys.exit(app.exec_())

How to check what is delaying the GUI response?

**edit

This is what i found as a result for ps aux | grep kworker : Please check the image attached. Some kworkers re-worked again the moment I opened the laptop again.
Moreover, I was not able to take a proper screenshot because the laptop is not responding very well.

CMD results

0

You could try powertop and iotop to check what is consuming most power and what is using the most disk IO respectively. Together with top those should be a decent indicator of what is actually happening under the hood.

0

Profile your python Code.
A profile is a set of statistics that describes how often and for how long various parts of the program executed. These statistics can be formatted into reports via the pstats module.

https://docs.python.org/3/library/profile.html

Make sure to clean up threads, resources and close all handles.

  • following your advice i tried .. python3 -m cprofile script.py .. however it did not show anything when the script was done. – x mohd Jun 18 at 5:54
  • did you instanciate the cProfile object and called enable(), disable() and the print_stats() methods within your python code ? – Michael D. Jun 18 at 20:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.