[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] pygame+threading+windows
On Thu, Aug 03, 2006 at 05:38:47PM +0200, Lars Friedrich wrote:
> > >
> > >> In general you must use pygame on the main thread. The same is true
> > >> with any cross-platform GUI. You should start other threads for the
> > >> other tasks your application is doing.
> > >
> > > This implies, that all user interfacing is done in the main thread.
> > > What is the usual way to deal with this in an object oriented
> > > approach?
> > >
> > > My plan was to have several classes that inherit from "Thread" and are
> > > each responsible for a certain part of the GUI. This way I have the
> > > possibility to easily switch one part of my GUI on or off by
> > > instantiating resp. deleting an object of the responsible class.
> > > Learning that one cannot delete Threads and that all GUI-event-
> > > handling
> > > should be done in the main Thread disappointed me little. What is the
> > > usual structure, if I want to have a highly configurable framework,
> > > with different GUI-Windows, not present in every application?
> >
> > Why do you think you need threads? Nothing you've said implies a need
> > or want for threads at all.
>
> I would like to explain my thoughts using a specific example of my
> project:
>
> I need to get frames from a camera and display them on the screen. At
> the same time, I have to do some other stuff. I wrote a "Camera"-class
> which has a "getFrame" method, returning a frame in a numpy-array. Now I
> also have a "CamWindow"-Class (that inherits from "Thread") which will
> know which camera's frames are to be displayed. It works like this:
>
> myCam = Camera(...various options...)
>
> myCamWindow = CamWindow(myCam)
>
> myCamWindow.start()
>
> Now the myCamWindow-Thread calls the "getFrame"-method of "myCam" and
> displays the data, using pygame. The "run"-method of "CamWindow"
> contains a "while 1"-loop with a "sleep"-command inside. If "CamWindow"
> would not be a "Thread", program excecution would stop when this loop is
> entered. That's why I wanted a "Thread".
>
> I would like to be able to use all these objects in Python's interactive
> mode, since I don't want to make a GUI for every little thing I would
> like to try out.
>
> Thanks for every comment
>
> Lars
>
>
You do not need threads for what you are trying to do.
Ditch the idea of having a CamWindow.run() method that does and endless
while loop.
It would be much better for you to provide a CamWindow.update() method
that just does the check for a new frame *once* and then call
CamWindow.update() periodically from you main pygame loop.
Or to put it another way, what is the point of having two threads that
both do:
while 1:
do_something
sleep
while 1:
do_something_else
sleep
When it is much easier to use a single non-threaded loop that does:
while 1:
do_something
do_something_else
sleep
There /are/ situations where threading is better, but from what you have
described, I don't think this is one of them.
---
James Paige