[Tutor] program hangs in while loop using wx.yield
Alan Gauld
alan.gauld at btinternet.com
Sun Nov 14 10:04:59 CET 2010
"Alex Hall" <mehgcap at gmail.com> wrote
> I had this working back in the summer, but have since had to
> restructure some things in the code. Now, my program is hanging
> while
> it waits for the human player to take his/her turn, and I am not
> sure
> how to free it;
I don't really understand this concept in a GUI?
Why would you be waitying for the user?
Surely you should just go back to the mainloop
and let the GUI control the users input? Then
after processing that call the computer move code.
Something like:
def handle_user_move_event():
processMoveOnGUI()
movedata = generateComputerMove()
processMoveOnGUI()
return
Thats what I would expect to see.
> while not self.grid.turnOver:
> print("in while") #just a test to make sure I got this far
> wx.Yield()
> time.sleep(1)
> #end while
So this is presumably buried in some very long and complex method
that you have written and you are briefly giving control back to
wxPython
before continuing your processing? Except that you appear to just
cycle continually giving back control? It doesn't seem to make sense.
> some of them change the Grid's "turnOver" variable. Therefore, I
> loop
> until I see that the user performed an action on the grid that sets
> the Grid's turnOver to true, at which point the loop stops and the
> other user (a computer in this case) can go.
Why are you doing all this inside ane event handler? Why don't
you just make your move and hand back to the GUI?
> The problem is that this loop makes the app hang,
> with Windows saying it is not responding. Of
> course, the user can now not do anything at all, so I have to use
> ctrl-c from the cmd window where I launched it to kill it.
No idea why its doing that. Have you tried playing with the
Yield parameter and return value to see if they give a clue?
But frankly I'd look to reconstruct the code to avoid the use
of Yield completely. Yield should almost be a last resort...
> time to call any Grid function. Is this solvable, or should I just
> use
> threads?
Based on the loop you posted you shouldn't need threads,
just exit your method and wait for the user to do something
and catch that event to restart your code. Leave it to the GUI.
HTH,
--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/
More information about the Tutor
mailing list