0
class Ui_Shop_Dashboard_Form(object):
    def setupUi(self, Shop_Dashboard_Form):
       Shop_Dashboard_Form.setObjectName("Shop_Dashboard_Form")
       Shop_Dashboard_Form.resize(750, 300)
       Shop_Dashboard_Form.setMaximumSize(QtCore.QSize(750, 300))
       font = QtGui.QFont()
       font.setBold(False)
       font.setWeight(50)
       Shop_Dashboard_Form.setFont(font)
       self.gridLayout = QtWidgets.QGridLayout(Shop_Dashboard_Form)
       self.gridLayout.setObjectName("gridLayout")
       BLA 
       BLA
       BLA
       #loop event for refreshing data
    #*********************************************************************           
    
    def printit():
        threading.Timer(5.0, printit).start()
        print ("Hello, World!")
    printit()

Hello, Quick question. I want to run a thread timer in my main application. I was wondering will the tread shut down when the application closes or does it continue to run in the back ground?

I will obviously replace the print ("Hello, World") with a call function to refresh data from the SQL server. But be before I do I want to be sure when the application closes that the thread will close with it. I do not want to continual refresh data from the server after the main window closes. I ran it in Visual studio codes and when I close the application it continues to print hello world in the terminal. so I am not confident it will close. can I get a bit of help on this as I am still new to Python and I have never worked with Treading before. If there is a smarter way to run a function every 30 minutes please let me know. Thank you in advance.

bruno
  • 32,421
  • 7
  • 25
  • 37
steve
  • 19
  • 3
  • If you decide you want to stop the thread, here are some possibilities: https://stackoverflow.com/questions/323972/is-there-any-way-to-kill-a-thread – Wups Oct 30 '20 at 18:39
  • Threads aren't killed, when the main thread exists, unless they are daemons. In case of the `Timer` object, you can call its `cancel()` method. – Wups Oct 31 '20 at 08:33

1 Answers1

1

Whatever the threads created by Python process or service will get killed if the application or process is killed properly. Also within application if the threads are handled correctly with the correct exceptions we dont need to worry about killing the thread.

In your case, I think Though you run the python code in VS code it internally creates the process in the machine which doesn't get killed after you close the VS Code application.

If there is a smarter way to run a function every 30 minutes? Yes, We can use the scheduler module to trigger a function for the way we want for example

import schedule
import time

def print_it():
    print("Hello, World!")


schedule.every(30).minutes.do(print_it)

while True:
    schedule.run_pending()
    time.sleep(1)


You can install schedule module using sudo pip install schedule. More Info about the schedule module: https://pypi.org/project/schedule/

Kovalan R
  • 110
  • 1
  • 1
  • 6
  • This kinda works I am sure I am not putting the code in the correct place for it to work. I have my window class class bla bla code ...... should the code go in the class itself or out side the class? – steve Oct 30 '20 at 19:22
  • It's a good practice to keep our logic within the class and use the class->methods to run it. – Kovalan R Oct 31 '20 at 04:18
  • Thanks everyone. I have to say everyone in this page is extremely helpful. Especially to people who are trying to learn. – steve Oct 31 '20 at 22:37
  • 1
    if this answer helped you can upvote/mark the answer as correct! – Kovalan R Nov 01 '20 at 10:26
  • yes this helped me figure out what I was doing wrong. – steve Nov 10 '20 at 17:05