Pet*_*per 2 python queue multiprocessing eoferror
我希望有一个进程持续监视 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
Run Code Online (Sandbox Code Playgroud)
#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()
Run Code Online (Sandbox Code Playgroud)
#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())
Run Code Online (Sandbox Code Playgroud)
请注意,我曾尝试在 test4.py 中的输入语句中添加一个 try:,在这种情况下,它会一直打印“输入内容,True 是关键字:”无限期地,没有 cr。
我在疯狂尝试修复它时添加了 Lock,没有任何区别
有人知道为什么会这样吗?
您的问题可以归结为一个更简单的脚本:
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()
Run Code Online (Sandbox Code Playgroud)
运行时,它产生
$ python3 i.py
Got ''
Run Code Online (Sandbox Code Playgroud)
读取零字节意味着管道已关闭并将input(..)其转换为EOFError异常。
该multiprocessing模块不允许您阅读stdin. 这通常是有道理的,因为将stdin多个孩子的读者混合在一起是一项冒险的工作。事实上,深入研究实现,multiprocessing/process.py明确设置stdin为devnull:
sys.stdin.close()
sys.stdin = open(os.devnull)
Run Code Online (Sandbox Code Playgroud)
如果您只是stdin用于测试,那么解决方案很简单:不要那样做!如果你真的需要用户输入,生活就会困难得多。您可以在父级中使用额外的队列和代码来提示用户并获取输入。