[英]Python 3: multiprocessing, EOFError: EOF when reading a line
我希望有一个过程可以持续监视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。
我在疯狂尝试中添加了锁定功能,没有区别
有人知道为什么会这样吗?
您的问题可以归结为一个简单的脚本:
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
进行测试,则解决方案很简单:不要这样做! 如果您确实需要用户输入,则生活会更加困难。 您可以在父级中使用其他队列以及代码来提示用户并获得输入。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.