Python 3:多处理,EOFError:读取一行时的EOF [英] Python 3: multiprocessing, EOFError: EOF when reading a line
问题描述
我希望有一个进程持续监视 RPi 输入,并将变量(我选择了一个队列)设置为 True 或 False 以反映去抖动值.然后另一个进程将捕获图像(从流中).我写了一些代码只是为了检查我可以让多处理和信号(队列)工作正常(我是一个业余编码员......).
I wish to have a process continually monitoring RPi input, and set a variable (I have chosen a queue) to True or False to reflect the debounced value. Another process will then capture an image (from a stream). I have written some code just to check I can get multiprocessing and signalling (the queue) working ok (I'm an amature coder...).
线程处理一切正常,但多处理会出现奇怪的错误.特别是多处理,EOFError:读取一行时的EOF".代码输出:-
It all works fine with threading, but multiprocessing is giving an odd error. Specifically 'multiprocessing, EOFError: EOF when reading a line'. Code outputs:-
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.
Note that I have tried adding a try: to the input statement in test4.py, in which case it keeps printing "enter something, True is the key word: " indefinitely, without a cr.
我疯狂地添加了 Lock 来修复它,没有任何区别
I added Lock in wild attempt to fix it, makes no difference
有人知道为什么会这样吗?
Anyone any idea why this is happening?
推荐答案
您的问题可以归结为一个更简单的脚本:
Your problem can be boiled down to a simpler script:
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
异常.
Reading zero bytes means the pipe is closed and input(..)
turns that into an EOFError
exception.
multiprocessing
模块不允许您阅读 stdin
.这通常是有道理的,因为混合来自多个孩子的 stdin
阅读器是一项冒险的工作.实际上,深入研究实现,multiprocessing/process.py
显式地将 stdin
设置为 devnull
:
The multiprocessing
module doesn't let you read stdin
. That makes sense generally because mixing stdin
readers from multiple children is a risky business. In fact, digging into the implementation, multiprocessing/process.py
explicitly sets stdin
to devnull
:
sys.stdin.close()
sys.stdin = open(os.devnull)
如果您只是使用 stdin
进行测试,那么解决方案很简单:不要那样做!如果你真的需要用户输入,生活就会困难得多.您可以在父级中使用额外的队列和代码来提示用户并获取输入.
If you are just using stdin
for test, then the solution is simple: Don't do that! If you really need user input, life is quite a bit more difficult. You can use additional queues plus code in the parent to prompt users and get input.
这篇关于Python 3:多处理,EOFError:读取一行时的EOF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!