python线程问题_python2.7中的线程问题

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代码在那里,它就可以正常运行。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值