独立同步输入单元

2024-04-18 00:08:14 发布

您现在位置:Python中文网/ 问答频道 /正文

在python代码中,我希望从串行端口读取数据,并通过键盘串行口和键盘上的数据可以随时来,并且彼此独立,我开始为这两个输入创建两个线程。在

import thread

def serialread():
 while 1:
  while(is.Waiting>0)
   out+=ser.read()
  print out
# now do something with the input 'out'

def keyboard():
 while 1:
  print(raw_input("Enter a Command"))
#now do something with the keyboard input

thread.start_new_thread(serialread,())
thread.start_new_thread(keyboard, ())

当我运行上面的代码时,终端在进入下一个迭代之前会同时等待键盘输入和串行读取,但是我希望当有串行读取时,只有那个线程可以工作,每当有键盘输入时,相应的线程应该工作,它不应该等待两个输入,然后只等待转到下一个迭代。在

这可能吗??我应该使用线程模块吗??在


Tags: 代码inputdefwith键盘out线程do

热门问题