我希望有一个过程不断监视RPi输入,并将变量(我选择了一个队列)设置为True或False以反映去抖动值。然后,另一个过程将捕获图像(来自流)。我已经写了一些代码来检查我可以得到多处理和信令(队列)工作正常(我是一个不成熟的编码器......)。
这一切都可以正常使用线程,但多处理会给出一个奇怪的错误。特别是多处理,EOFError:读取一行时的EOF'。代码输出: -
this computer has the following number of CPU's 6
OK, started thread on separate processor, now we monitor variable
enter something, True is the key word:
Process Process-1:
Traceback (most recent call last):
File "c:\Python34\lib\multiprocessing\process.py", line 254, in _bootstrap
self.run()
File "c:\Python34\lib\multiprocessing\process.py", line 93, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Peter\Documents\NetBeansProjects\test_area\src\test4.py", line 16, in Wait4InputIsTrue
ValueIs = input("enter something, True is the key word: ")
EOFError: EOF when reading a line
#test4.py
from time import sleep
from multiprocessing import Lock
def Wait4InputIsTrue(TheVar, TheLock):
while True:
sleep(0.2)
TheLock.acquire()
#try:
ValueIs = input("enter something, True is the key word: ")
#except:
# ValueIs = False
if ValueIs == "True":
TheVar.put(True)
print("changed TheVar to True")
TheLock.release()
#test5.py
if __name__ == "__main__":
from multiprocessing import Process, Queue, Lock, cpu_count
from time import sleep
from test4 import Wait4InputIsTrue
print("this computer has the following number of CPU's", cpu_count())
LockIt = Lock()
IsItTrue = Queue(maxsize = 3)
Wait4 = Process(target = Wait4InputIsTrue, args = (IsItTrue, LockIt))
Wait4.start()
print("OK, started thread on separate processor, now we monitor variable")
while True:
if IsItTrue.qsize():
sleep(0.1)
print("received input from separate thread:", IsItTrue.get())
请注意,我已尝试在test4.py中的输入语句中添加try:,在这种情况下,它会继续打印"输入内容,True是关键字:"无限期地,没有cr。
我在疯狂尝试修复它时添加了Lock,没有任何区别
有人知道为什么会这样吗?
答案 0 :(得分:2)
您的问题可归结为更简单的脚本:
import multiprocessing as mp
import sys
def worker():
print("Got", repr(sys.stdin.read(1)))
if __name__ == "__main__":
process = mp.Process(target=worker)
process.start()
process.join()
运行时会生成
$ python3 i.py
Got ''
读取零字节表示管道已关闭,input(..)
将其转换为EOFError
例外。
multiprocessing
模块不允许您阅读stdin
。这通常是有道理的,因为混合来自多个孩子的stdin
读者是一项有风险的事情。实际上,在深入了解实现时,multiprocessing/process.py
明确将stdin
设置为devnull
:
sys.stdin.close()
sys.stdin = open(os.devnull)
如果您只是使用stdin
进行测试,那么解决方案很简单:不要这样做!如果你真的需要用户输入,生活会变得更加困难。您可以在父级中使用其他队列和代码来提示用户并获取输入。