Created
July 16, 2013 19:29
-
-
Save thomasballinger/6011843 to your computer and use it in GitHub Desktop.
Python threads and file io
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
import tty | |
import Queue | |
import threading | |
import time | |
tty.setcbreak(sys.stdin) | |
events = Queue.Queue() | |
def get_input(): | |
while True: | |
print 'before read' | |
c = sys.stdin.read(1) | |
print 'after read' | |
print 'adding event to queue' | |
events.put(c) | |
t = threading.Thread(target=get_input) | |
t.daemon = True | |
t.start() | |
while True: | |
p = open('file.txt', 'w') | |
event = None | |
try: | |
event = events.get_nowait() | |
except Queue.Empty: | |
pass | |
time.sleep(.01) | |
if event: | |
print 'key pressed:', event | |
else: | |
print '.', | |
sys.stdout.flush() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment