3

I have a continuous loop that modifies data in an array and pauses for one second on every loop. Which is no problem..but I also need to have to print a specific part of the array to the screen on a specific keypress is entered, without interrupting the continuous loop running in one second intervals.

Any ideas on how to get the keypress while not distrupting the loop?

Steven Lutz
  • 467
  • 1
  • 6
  • 16
  • I've never done this outside of a windowing system but this post seems relevant: http://stackoverflow.com/questions/12175964/python-method-for-reading-keypress – user2027202827 Jul 04 '15 at 04:09

2 Answers2

1

You can use either multiprocessing or threading library in order to spawn a new process/thread that will run the continuos loop, and continue the main flow with reading the user input (print a specific part of the array to the screen etc).

Example:

import threading

def loop():
    for i in range(3):
        print "running in a loop"
        sleep(3)
    print "success"

if __name__ == '__main__':

    t = threading.Thread(target=loop)
    t.start()
    user_input = raw_input("Please enter a value:")
    print user_input
    t.join()
Nir Alfasi
  • 53,191
  • 11
  • 86
  • 129
  • This looks like it's along the lines of what I need. Do you have a document that explains this or can you at least explain the target =loop part? – Steven Lutz Jul 04 '15 at 07:00
  • @StevenLutz `target=loop` means that the thread that'll get spawned will run the function `loop()` when the `start()` function gets called. `join()` means waiting till the thread has done running its task. – Nir Alfasi Jul 04 '15 at 16:02
  • I'm understanding this a bit better now. but I have one last question. take a look at this code: import threading import time def loop(): count = 0 while True: count += 1 print "This is a Loop" time.sleep(1) print "success" if __name__ == '__main__': t = threading.Thread(target=loop) t.start() while True: user_input = raw_input("Please enter a value:") print "User input: " + user_input print "Current count: " + count t.join() How do I get count to be passed outside the thread? I'm getting a count isn't defined err. – Steven Lutz Jul 29 '15 at 04:43
  • @StevenLutz Please post a *new* question (in case this *is* a new question) or update the current one (in case this is only an update that adds information and doesn't change the original question). Good luck! – Nir Alfasi Jul 29 '15 at 04:47
  • After posting that I realized I should probably start a new question, which I'm doing now. – Steven Lutz Jul 29 '15 at 04:48
  • @StevenLutz (thumbsup) – Nir Alfasi Jul 29 '15 at 05:00
1

You're probably looking for the select module. Here's a tutorial on waiting for I/O.

For the purpose of doing something on keypress, you could use something like:

import sys
from select import select

# Main loop
while True:
    # Check if something has been input. If so, exit.
    if sys.stdin in select([sys.stdin, ], [], [], 0)[0]:
        # Absorb the input
        inpt = sys.stdin.readline()
        # Do something...
Praveen
  • 6,872
  • 3
  • 43
  • 62
  • Sorry, I guess I didn't read your question completely. This tells you how to break the loop, but not how to keep the one-second interval. If you do something else, you're not counting down, so you'd probably still need to spawn a new process or something, as another answer suggested. Thought this answer might still be of some value, though. – Praveen Jul 04 '15 at 05:14