The way a function that is run in a Worker receives one of the progress_callback, status_callback and message_callback is rather intricate and requires an explanation.
To run a function in a separate thread we use a Worker which is a subclass of a QRunnable.
For the Worker to work one needs to define:
the function that does what you need
Optional callback methods to get the status of the thread by means of QtCore.QSignals
On initialisation of the Worker the user needs to pass the function that has to run in the thread, i.e. fn below, and additional optional positional and keyword arguments, which will be passed on to the actual function that is run in the QRunnable.
Therefore it is advisable to always have **kwargs in the function fn signature so that you can access the QSignal and emit the signal required. For instance one could emit a progress by
def fn(num_iter, **kwargs):
progress_callback = kwargs.get('progress_callback', None)
for i in range(num_iter):
do_something
if progress_callback is not None:
progress_callback.emit( i )
But how are the signal passed to the Worker anyway?
This is done just after one has defined the Worker:
def handle_progress(num_iter):
# do something with the progress
print ("Current progress is ", num_iter)
worker = Worker(fn, 10)
worker.signals.progress.connect(handle_progress)
The way a function that is run in a
Worker
receives one of theprogress_callback
,status_callback
andmessage_callback
is rather intricate and requires an explanation.To run a function in a separate thread we use a
Worker
which is a subclass of aQRunnable
.For the
Worker
to work one needs to define:QtCore.QSignal
sOn initialisation of the
Worker
the user needs to pass the function that has to run in the thread, i.e.fn
below, and additional optional positional and keyword arguments, which will be passed on to the actual function that is run in theQRunnable
.https://github.com/paskino/qt-elements/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L32
In practice the user will need to pass to the
Worker
as many parameters as there are listed in the function to be run.https://github.com/paskino/qt-elements/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L56
But before running that
Worker
will add to the**kwargs
the followingQSignal
.https://github.com/paskino/qt-elements/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L41-L43
Therefore it is advisable to always have
**kwargs
in the functionfn
signature so that you can access theQSignal
and emit the signal required. For instance one could emit a progress byBut how are the signal passed to the Worker anyway?
This is done just after one has defined the
Worker
:What are the available signals?
https://github.com/paskino/qt-elements/blob/535e487d09d928713d7d6aa1123657597627c4b0/eqt/threading/QtThreading.py#L83-L89