Start a new thread in Python with a callback method in Main Thread for PyQT application
Date : March 29 2020, 07:55 AM
I wish this help you You need to use QThread and signal and slots. QThread inherits from QObject, which allows to emit signals. Upon the completion of the task, the QThread will emit a finished() signal class CustomThread(QtCore.QThread):
def __init__(self, target, slotOnFinished=None):
super(CustomThread, self).__init__()
self.target = target
if slotOnFinished:
self.finished.connect(slotOnFinished)
def run(self, *args, **kwargs):
self.target(*args, **kwargs)
class MyCustomWidget(QtGui.QWidget):
def __init__(self, parent=None):
super(MyCustomWidget, self).__init__(parent)
layout = QtGui.QVBoxLayout(self)
button = QtGui.QPushButton("Start", self)
layout.addWidget(button)
button.clicked.connect(self.onStart)
self.actionthread = CustomThread(target=self.longAction, self.onFinished)
def onFinished(self):
# Do Something
def longAction(self):
time.sleep(3)
def onStart(self):
self.actionthread.start()
|
Python threading.thread.start() doesn't return control to main thread
Tag : python , By : nickthecook
Date : March 29 2020, 07:55 AM
seems to work fine Thread.start() never returns! Could this have something to do with the C implementation of the math library? print(message) # works
|
Python - how to do not start thread on build thread step
Date : March 29 2020, 07:55 AM
it should still fix some issue The problem is that you are invoking (calling) the function instead of passing it to the Thread class, so instead of t = threading.Thread(target=geter(token), args=(token,))
t = threading.Thread(target=geter, args=(token,))
|
Python 3.6 - How to start a thread from within a daemon thread and have sub-thread exit once the function it is running
Date : March 29 2020, 07:55 AM
I wish this help you After much searching and investigation I was able to find a solution to the issue by using information in this stackoverflow answer on threading Basically I found that I was able to start a new thread from within a thread using the code below: t = threading.Thread(target=target_function,args=(args))
t.daemon = True
t.start()
import threading
threads = []
def func1():
// runs in its own thread and receives messages from a message queue
def func2():
// runs in its own thread and sends messages in a loop to a message queue
def func3():
// runs in its own thread and receives messages from a message queue, does some
// processing, assigns value to variable_from_func3
t = threading.Thread(target=func4,args=(variable_from_func3))
t.daemon = True
t.start()
// Code above starts func4 in its own thread passing variable_from_func3 as an arg
def func4(variable_from func3):
// performs actions on variable passed from func3
// This now runs in its own thread which dies once complete
def manager():
# Thread t1
t1 = threading.Thread(target=func1)
t1.daemon = True
threads.append(t1)
# Thread t2
t2 = threading.Thread(target=func2)
t2.daemon = True
threads.append(t2)
t2.start()
# Thread t3
t3 = threading.Thread(target=func3)
t3.daemon = True
threads.append(t3)
t3.start()
t1.start()
for t in threads:
t.join()
manager()
|
Python: I start a new thread, and my program pauses until the thread is finished
Date : March 29 2020, 07:55 AM
I think the issue was by ths following , When I try to start a new thread, my entire program stops until the thread's function finishes. I am trying to make the thread start and continue while my program runs at the same time. , By doing do_python("key = cpc.get_key()")
getKeyThread = threading.Thread(target=do_python, args=some_args, daemon=True).start()
|