Results 1 to 4 of 4

Thread: How to do this in Python?

  1. #1
    Join Date
    Apr 2007
    Location
    Lower 48, US
    Beans
    511
    Distro
    Ubuntu 11.04 Natty Narwhal

    Question How to do this in Python?

    How do I make Python listen for external input while running? I don't mean "raw_input()", I mean where the program will be running happily, and then if I press "q" while its running, it quits... Or something along those lines...

  2. #2
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: How to do this in Python?

    simplest method is to catch the KeyboardInterrupt exception that is generated when the user presses ctrl+C

    Code:
    try:
      code_that_takes_long_time()
    except KeyboardInterrupt:
      print "stopped"
      clean_up()
    otherwise you may need to look at a terminal handling library like curses.

  3. #3
    Join Date
    Apr 2007
    Location
    Lower 48, US
    Beans
    511
    Distro
    Ubuntu 11.04 Natty Narwhal

    Re: How to do this in Python?

    Quote Originally Posted by ssam View Post
    simplest method is to catch the KeyboardInterrupt exception that is generated when the user presses ctrl+C

    Code:
    try:
      code_that_takes_long_time()
    except KeyboardInterrupt:
      print "stopped"
      clean_up()
    otherwise you may need to look at a terminal handling library like curses.
    So there is no way to do two things at once in Python?

  4. #4
    Join Date
    Dec 2004
    Location
    Manchester
    Beans
    2,086
    Distro
    Ubuntu Mate 15.10 Wily Werewolf

    Re: How to do this in Python?

    you can use threads.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •