Skip to content

Instantly share code, notes, and snippets.

@thomasballinger
Created July 16, 2013 19:29
Show Gist options
  • Save thomasballinger/6011843 to your computer and use it in GitHub Desktop.
Save thomasballinger/6011843 to your computer and use it in GitHub Desktop.
Python threads and file io
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