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 ())
当我运行上面所示的代码时,它很高兴地完成了它的任务,即测量两次击键之间的时间,并给出度量中最不重要的3个数字。在
但是,当我取出这部分时(因为我不需要也不一定想要):
^{pr2}$
它断了。我得到以下错误代码:Traceback (most recent call last):
File "randgen.py", line 50, in
thread.start_new_thread(tm ())
TypeError: start_new_thread expected at least 2 arguments, got 1
更新
当我在thread.start_new_thread(tm, ())中添加逗号时,会出现以下错误:
Unhandled exception in thread started by
sys.excepthook is missing
lost sys.stderr
但是,当逗号丢失时,只要while True代码在那里,它就可以正常运行。在