我希望有一个进程持续监控 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 in wild 尝试修复它,没有区别
有人知道为什么会这样吗?