import time
import thread
import termios
import sys
import datetime
try:
from msvcrt import getch # try to import Windows version
except ImportError:
def getch(): # define non-Windows version
import tty, termios
fd = sys.stdin.fileno()
old_settings = termios.tcgetattr(fd)
try:
tty.setraw(sys.stdin.fileno())
ch = sys.stdin.read(1)
finally:
termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
return ch
def tm():
char = None
def keypress():
count=0
while count<5:
a=time.time()
global char
char = getch()
b=time.time()
c=b-a
c=c*10000000000000000
c=int (c)
c=c%1000
print c
count+=1
thread.start_new_thread(keypress, ())
while True:
'''if char is not None:
print("Key pressed is " + char.decode('utf-8'))
break'''
print("Program is running")
time.sleep(5)
thread.start_new_thread(tm ())
when I run the code as shown above, it happily does what it's meant to do, which is measure the time in between keystrokes and give the 3 least significant numbers in the measurement.
However, when I take out this part (because I don't need nor necessarily want it there):
while True:
'''if char is not None:
print("Key pressed is " + char.decode('utf-8'))
break'''
print("Program is running")
time.sleep(5)
It breaks. I get the following error code:
Traceback (most recent call last):
File "randgen.py", line 50, in <module>
thread.start_new_thread(tm ())
TypeError: start_new_thread expected at least 2 arguments, got 1
Update
when I add a comma to the thread.start_new_thread(tm, ())
I get this error:
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
However, when the comma is missing, it runs fine as long as that piece of while True
code is there.