Essentially what I want to do is have a function that whenever it is called, it is executed on a separate thread. I do not want to call thread.start(), as this executes it immediately.
I have the following python script. I am using PyQt5 for my GUI. Whenever a button is pressed, I want to execute a long running function on a separate thread. here is the code:
from PyQt5 import QtWidgets, uic
import time
def longRunningFunction():
time.sleep(10)
app = QtWidgets.QApplication([])
dlg = uic.loadUi("app.ui")
dlg.button_to_click.clicked.connect(longRunningFunction)
dlg.show()
app.exec()
When I add threads to the program, the function is executed on thread.start(). here is the code below, with threading added:
from PyQt5 import QtWidgets, uic
import time
import threading
def longRunningFunction():
time.sleep(10)
app = QtWidgets.QApplication([])
dlg = uic.loadUi("app.ui")
dlg.button_to_click.clicked.connect(longRunningFunction)
thread = threading.Thread(target=longRunningFunction)
thread.start()
dlg.show()
app.exec()
What is the best way that I can make the function always use a separate thread when it is called, so that the GUI does not freeze up? This function will be called continuously while running, several times each minute. This function will also be much more complex than the example. It will use multiple methods from other custom made python scripts and also have a websocket connection running.