当我与包并行运行我的test_cases时<multiprocessing>,它会抛出值错误:关闭文件的I / O操作



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:Python27libmultiprocessingprocess.py", line 232, in _bootstrap
      self.run()
     File "C:Python27libmultiprocessingprocess.py", line 88, in run
      self._target(*self._args, **self._kwargs)
     File "C:Python27libHTMLTestRunner.py", line 630, in run
      self.generateReport(test, result)
     File "C:Python27libHTMLTestRunner.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

希望这有帮助

相关内容

  • 没有找到相关文章

最新更新