Python is too fast !

F

Fuzzyman

I've written an anagram finder that uses recursion and a large
wordlist to find anagrams of input words you give it (e.g. 'michael
food' = 'cool firm head') ;-)

http://www.voidspace.org.uk/atlantibots/nanagram.html

It has a nice Tkinter front end and works very well, *except* if you
give it a long input it takes a long time to process (as might be
expected) - ten minutes or more sometimes before it returns with the
answers.

I've done a CGI version which returns the answers *whilst* it is
finding them ( http://www.voidspace.xennos.com/cgi-bin/Nanagram-CGI.py
) - which is a much slower method, but less boring to watch.

So I've coded an option - which opens a temporary window with a
Tkinter text widget in a frame and outputs the answers to the window
as it finds them. The trouble is that the recursion is very processor
intensive and it 'freezes' the window and it doesn't update. (I'm
running on an AMD XP1700 with Win XP).

*If* I run this from IDLE and put a print statement into the loop as
well as output to the text widget, then the print statement seems to
free up the widget and it works as it should. I suspect that the print
statement is making system calls which free up the system long enough
to update the screen. When I run it without a console - the benefit is
lost and it freezes again. However I've tried adding a sleep statement
( 0.001, 0.01 and even 0.1 of a second) and this has no effect (other
than slowing down the loop without unfreezing the window).

I'm looking for another suggestion to 'waste time' - preferably in a
'non-processor-intensive' way, that returns control to the sytem for
an instant ? (also preferably cross platform).

If you have a suggestion it would be helpful if you could e-mail it to
me at fuzzyman AT atlantibots DOT org DOT uk as well as post it here -
because I only have intermittent access to the net.


MANY THANKS


Fuzzyman

http://www.voidspace.org.uk/atlantibots/pythonutils.html
 
R

Roger Binns

So I've coded an option - which opens a temporary window with a
Tkinter text widget in a frame and outputs the answers to the window
as it finds them. The trouble is that the recursion is very processor
intensive and it 'freezes' the window and it doesn't update. (I'm
running on an AMD XP1700 with Win XP).

You have two choices as to how to solve this. One is turn your
code into a generator. The gui code then becomes something like:

for word in genanagrams():
<display the anagram>
<possibly call Yield in gui toolkit>

Your other choice is to put your anagram generation into a background
thread, and have it send updates to the main (gui) thread. I have long
since given up on TkInter and use wxPython instead. In wxPython you
can use wx.PostEvent to send updates from worker threads to the
gui thread.

To send information from the main (gui) thread, use a Queue.Queue.
Call put() to add stuff in the main thread, and in the worker threads
call get() to get what they should do. Generally you would use
some sort of message object.

Roger
 
C

Cameron Laird

I've written an anagram finder that uses recursion and a large
wordlist to find anagrams of input words you give it (e.g. 'michael
food' = 'cool firm head') ;-)

http://www.voidspace.org.uk/atlantibots/nanagram.html

It has a nice Tkinter front end and works very well, *except* if you
give it a long input it takes a long time to process (as might be
expected) - ten minutes or more sometimes before it returns with the
answers.

I've done a CGI version which returns the answers *whilst* it is
finding them ( http://www.voidspace.xennos.com/cgi-bin/Nanagram-CGI.py
) - which is a much slower method, but less boring to watch.

So I've coded an option - which opens a temporary window with a
Tkinter text widget in a frame and outputs the answers to the window
as it finds them. The trouble is that the recursion is very processor
intensive and it 'freezes' the window and it doesn't update. (I'm
running on an AMD XP1700 with Win XP).

*If* I run this from IDLE and put a print statement into the loop as
well as output to the text widget, then the print statement seems to
free up the widget and it works as it should. I suspect that the print
statement is making system calls which free up the system long enough
to update the screen. When I run it without a console - the benefit is
lost and it freezes again. However I've tried adding a sleep statement
( 0.001, 0.01 and even 0.1 of a second) and this has no effect (other
than slowing down the loop without unfreezing the window).

I'm looking for another suggestion to 'waste time' - preferably in a
'non-processor-intensive' way, that returns control to the sytem for
an instant ? (also preferably cross platform).
.
.
.
1. I haven't looked at your code.
2. I think you're describing an aspect of Tkinter
programming that's puzzled many of us. Calling
it a way to "waste time" probably doesn't get
your point across, though.
3. The simple-minded answer I believe you're after
is: use update() <URL:
http://www.pythonware.com/library/tkinter/introduction/x9374-event-processing.htm >
4. The Tk crowd has a lot to say on this subject
<URL: http://wiki.tcl.tk/1526 >.
 
F

Fuzzyman

[email protected] (Fuzzyman) wrote in message news: said:
I'm looking for another suggestion to 'waste time' - preferably in a
'non-processor-intensive' way, that returns control to the sytem for
an instant ? (also preferably cross platform).

If you have a suggestion it would be helpful if you could e-mail it to
me at fuzzyman AT atlantibots DOT org DOT uk as well as post it here -
because I only have intermittent access to the net.

I think I was fooled by the fact that adding the print statement
seemed to free up the window. (It was originally only added as a
trace).

What I really wanted to do was to force the window to refresh - a bit
of googling on the subject (and a helpful email from Cameron Laird)
brought me to the Tk update() method which works great.

Unfortunately I can't FTP the code until Monday... but I'm very
pelased with it......

Fuzzy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,163
Latest member
Sasha15427
Top