Python is too fast !

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fuzzyman

    Python is too fast !

    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') ;-)



    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


  • Cameron Laird

    #2
    Re: Python is too fast !

    In article <8089854e.04022 10039.5b761d0f@ posting.google. com>,
    Fuzzyman <michael@foord. net> wrote:[color=blue]
    >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).[/color]

    Comment

    • Fuzzyman

      #3
      Re: Python is too fast !

      michael@foord.n et (Fuzzyman) wrote in message news:<8089854e. 0402210039.5b76 1d0f@posting.go ogle.com>...
      [snip..][color=blue]
      >
      > 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.
      >
      >[/color]

      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


      [color=blue]
      > MANY THANKS
      >
      >
      > Fuzzyman
      >
      > http://www.voidspace.org.uk/atlantib...thonutils.html[/color]

      Comment

      • Roger Binns

        #4
        Re: Python is too fast !

        > So I've coded an option - which opens a temporary window with a[color=blue]
        > 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).[/color]

        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


        Comment

        Working...