[PyQt] Question on QThread methodology/idiom/conceptual use
rob at gulon.co.uk
Thu Feb 11 11:17:20 GMT 2016
So, first things first: there are lots of wrong and very few (only one?)
right way to use QThread which is, as you have eluded to, to use
moveToThread() and not subclass QThread itself. The next most important
thing is that you can *only* use the signal slot mechanism to
communicate with your threaded object and these must be connected
*before* the thread is started. All slots should be appropriately
decorated with @pyqtSlot. Also important is that the object that is
being threaded should not have a parent.
I've written a simple gist
demonstrates these principles. Its a simple GUI that uses a threaded
object to calculate the nth prime as requested by the user. If you enter
a value of n of about 3000 it will take a few seconds to calculate in
the background and you will see that the GUI is still animating the
progress bar and stays responsive generally. You can request a prime as
many times as you like and the thread stays running without a single
run() function that terminates the thread on completion.
This second gist
(https://gist.github.com/jazzycamel/06b85f2dbad38a640a11) show an
example of a threaded object with a method containing an endless loop
that can be started/stopped/restarted via a Boolean flag. It uses slots
to change the flag and these are allowed to execute, even while the loop
is running, by the use of qApp.processEvents(). Using this mechanism it
would be possible to have a thread capable of running
(non-contemporaneously) a number of different long running, short loop
methods as you describe which could be started and stopped at will, you
would just need a marshaling system to ensure only one method is started
at a time. If you want to run multiple functions at the same time then
you will need multiple instances of the thread/threaded object.
I hope this helps,
On 10/02/2016 23:34, Ira Gray wrote:
> I'm currently at work so I don't have code I can share right this
> second, but I had a sort of epiphany moment and I'm hoping to get some
> clarification with how threading is normally intended to be executed
> in a Qt app.
> I'm using Python3 and PyQt5. I've been trying to make signals and
> slots and moveToThread work for a couple days now and not having a lot
> of success. I can implement the toy examples I find around the web,
> but when I try to re-engineer the examples to do what I want, things
> start to break down.
> What I've been trying to do is create two classes. One class handles a
> very basic UI consisting of a couple buttons, and the other class
> houses a couple recursive functions. Long running, but shortish loops.
> My goal/idea was I'd have two threads. One main thread for the UI, and
> a second thread for the long running functions. Pressing a button on
> the UI would start/stop the functions dynamically, but the thread
> would constantly be running regardless of if it had meaningful things
> to do or not.
> Based on the examples I'd read on moveToThread, I thought this would
> be somewhat simple. Setup some signals to pass information back and
> forth, instantiate my two classes, and then pass the worker class off
> to a thread, and presto I'm off to the races.
> Only, I haven't been able to get it to work, and every example I've
> found of moveToThread is moving an object with only one
> function/member, usually called run.
> Which brings me to my question:
> *Does the idea of having a class that holds multiple functions live in
> another thread permanently make sense? Can a thread even handle that?
> Is there a better way?*
> Thanks for any input,
> I'm mostly self taught, so I'm worried I picked up a misconception
> somewhere or I'm lacking some critical understanding of what's
> actually going on under the hood.
> PyQt mailing list PyQt at riverbankcomputing.com
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the PyQt