popen.communate提高了ValueError:' i/o操作在封闭文件上'
运行代码时,我会遇到以下错误。
Uncaught exception <class 'ValueError'>: I/O operation on closed file.Traceback (most recent call last): File "/home/gidumah/miniconda/envs/ytune/lib/python3.7/runpy.py", line 193, in _run_module_as_main "__main__", mod_spec) File "/home/gidumah/miniconda/envs/ytune/lib/python3.7/runpy.py", line 85, in _run_code exec(code, run_globals) File "/home/gidumah/ytopt/ytopt/ytopt/search/ambs.py", line 128, in <module> search.main() File "/home/gidumah/ytopt/ytopt/ytopt/search/ambs.py", line 105, in main results = list(self.evaluator.get_finished_evals()) File "/home/gidumah/ytopt/ytopt/ytopt/evaluator/evaluate.py", line 200, in get_finished_evals y = future.result() File "/home/gidumah/ytopt/ytopt/ytopt/evaluator/subprocess_evaluator.py", line 41, in result stdout, stderr_data = self.proc.communicate() File "/home/gidumah/miniconda/envs/ytune/lib/python3.7/subprocess.py", line 951, in communicate stdout = self.stdout.read() ValueError: I/O operation on closed file.
我正在运行的代码是 subprocess_evaluator.py
,如下:
class PopenFuture:
FAIL_RETURN_VALUE = Evaluator.FAIL_RETURN_VALUE
def __init__(self, args, parse_fxn):
self.proc = subprocess.Popen(args, shell=True, stdout=subprocess.PIPE,
stderr=subprocess.STDOUT, encoding='utf-8')
self._state = 'active'
self._result = None
self._parse = parse_fxn
def _poll(self):
if not self._state == 'active':
return
retcode = self.proc.poll()
if retcode is None:
self._state = 'active'
stdout, stderr_data = self.proc.communicate()
tmp_res = self._parse(stdout)
if tmp_res != sys.float_info.max:
self._result = tmp_res
elif retcode == 0:
self._state = 'done'
else:
self._state = 'failed'
def result(self):
if self._result is not None:
return self._result
self.proc.wait()
stdout, stderr_data = self.proc.communicate()
if self.done:
self._result = self._parse(stdout)
else:
self._result = self.FAIL_RETURN_VALUE
logger.error(f"Eval failed: {stdout}")
####
# if stdout:
# print (stdout)#.split('\n')[:-2])#)[:-1])
####
return self._result
它说该错误在第41行中,以 stdout,stderr_data = self.proc.communate()
stdout。 /code>
我在通信通话中没有做正确的事情吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
请注意,它等待该过程终止。因此,
popen.communate
不能多次运行。popen.communate
的上下文中的错误意味着该程序已经终止了该过程并关闭了管道,并在第二通信
呼叫时提高了例外。看来您正在使用 ytopt 库。请检查您是否正确使用代码,请注意,在几个不同的位置调用
communicate()
,有时会通过valueerrors传递,但有时不会传递。另外,请查看Thomas Wouters 解决方案解决问题:Accoring to the documentation:
Note that it waits for the process to terminate. Therefore
Popen.communicate
cannot be run more than once. The error in the context ofPopen.communicate
means that the program already terminated the process and closed the pipes, and raised the exception upon secondcommunicate
call.It seems that you are using code of the
ytopt
library. Please check if you use the code correctly, note that it callscommunicate()
in few different places, sometimes with passing on ValueErrors but sometimes not. Also, check out Thomas Wouters solution to the problem: