Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 独立同时输入_Python_Multithreading_Serial Port - Fatal编程技术网

Python 独立同时输入

Python 独立同时输入,python,multithreading,serial-port,Python,Multithreading,Serial Port,在我的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:

在我的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, ())
当我运行上述代码时,终端在进入下一个迭代之前等待键盘输入和串行读取,但我希望当有串行读取时,只有该线程应该工作,当有键盘inp时,相应的线程应该工作,它不应该等待两个输入,然后只进入下一个迭代


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

线程模块是一个低级模块,您不应该使用它。在python3中,它甚至被重命名为
\u thread
(注意,在python中,有一个惯例是使用
\u name\u以下划线开头来表示私有属性/对象)。但是我不明白你的问题。您的代码应该可以正常工作。您能否显示您正在提供的输入,并准确解释程序的行为(以及您预期的行为)?抱歉,不清楚。例如,如果有串行输入“启动操作1”且没有键盘输入,则程序应在不等待键盘输入的情况下启动“操作1”。。但在上面的代码中,除非有键盘输入,否则操作1也不会启动。我的意思是,我希望两个输入彼此独立运行,并且来自各自输入的处理也应该是独立的。希望这次我说得很清楚。我应该为此使用多处理吗。。??