gpt4 book ai didi

python - 当我与包 并行运行 test_cases 时,它抛出 ValueError : I/O operation on closed file

转载 作者:行者123 更新时间:2023-12-01 09:32:32 25 4
gpt4 key购买 nike

testrun.py(我将所有测试用例组合在一起以并行运行它们)片段....

    @staticmethod
def runcase(suite,processdir):
processlist = []
now = time.strftime('%Y-%m-%d-%H_%M_%S',time.localtime(time.time()))
test_result = 'C:/Users/huanri/Documents/apa_auto/test_report/' + now
+'_test_result.html'

fp = file(test_result, 'a')

print fp.closed
for i in suite:
# runner = unittest.TextTestRunner()
t = 0
runner = HTMLTestRunner.HTMLTestRunner(
stream = fp,
title = u'APA_Test_Report',
description = u'Test Case Excution Report'
)
proc = multiprocessing.Process(target = runner.run, args = (i,))
# print proc
processlist.append(proc)
t = t+1
# print processlist
for i in processlist:
i.start()
for i in processlist:
i.join(6)
fp.close()

...

执行 testrun.py 后,它会抛出下面

   Traceback (most recent call last):
File "C:\Python27\lib\multiprocessing\process.py", line 232, in _bootstrap
self.run()
File "C:\Python27\lib\multiprocessing\process.py", line 88, in run
self._target(*self._args, **self._kwargs)
File "C:\Python27\lib\HTMLTestRunner.py", line 630, in run
self.generateReport(test, result)
File "C:\Python27\lib\HTMLTestRunner.py", line 687, in generateReport
self.stream.write(output.encode('utf8'))
ValueError: I/O operation on closed file

我关闭文件时谁能指点一下?

最佳答案

我建议您在线程中打开和关闭文件。当您启动非阻塞线程时,过程代码将移至下一行并调用 fp.close()。所以你会看到这个异常。您可以通过打开和关闭每个 runner.run 中的文件来解决此问题。

这里有一些示例代码来了解它是如何工作的。 https://arstechnica.com/civis/viewtopic.php?t=1234639

希望这有帮助

关于python - 当我与包 <multiprocessing> 并行运行 test_cases 时,它抛出 ValueError : I/O operation on closed file,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49840417/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com