#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    12

    Accept user input while looping


    Guys, Im trying to do something like... loop and process reports, while constantly listening for user input, if user press "c" on keyboard, then interrupts the report processing, if press somekey then do something, izzit possible?
    What can change the nature of a man?
  2. #2
  3. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    This would be a good candidate for the use of threads.

    I would use the main thread to manage the user input and launch a second thread to process the files.

    Checkout the threading module.

    grim
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2004
    Location
    There where the rabbits jump
    Posts
    556
    Rep Power
    11
    Ya threads are the way but id make a second thread do the user input because by me it always does something wierd so it does not accept user input in the main thread
    Those people who think they know everything are a great annoyance to those of us who do.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Location
    Questioner's home
    Posts
    89
    Rep Power
    12
    Cool thanks, but is there a way to terminate a running thread? I need to interrupt a process during a loop, I cant seemed to find anything on that in the documentation.
    What can change the nature of a man?
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Originally Posted by Questioner
    Cool thanks, but is there a way to terminate a running thread? I need to interrupt a process during a loop, I cant seemed to find anything on that in the documentation.
    The file processing loop should check for a flag to be set periodically and then terminate. Any other way tends to be unstable in my experience. Basically threads are cooperative.

    Note:Just be careful, things like time.sleep() seem to halt the whole process and not just the thread.

    Instead of threads you could look at the signal module to handle CTRL-C.

    grim

IMN logo majestic logo threadwatch logo seochat tools logo