我希望有一个进程持续监视 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,没有什么区别
任何人都知道为什么会发生这种情况?