解析xml fi时出现nio错误

2024-09-24 16:31:20 发布

您现在位置:Python中文网/ 问答频道 /正文

我在Jython中有一个函数,这个函数使用Popen运行另一个程序,它将一个xml文件写入它的stdout,它被指向一个文件。当这个过程完成后,我关闭文件并调用另一个函数来解析它。在解析过程中,我收到了一堆错误消息,涉及到对关闭的文件和/或格式不正确的xml文件的访问(当我查看它们时,这些文件看起来很好)。我以为输出.关闭()可能在关闭文件之前返回,因此我添加了一个等待的循环输出.关闭说实话。一开始似乎有用,但后来我的程序打印了以下内容

blasting  
blasted  
parsing  
parsed  
    Extending genes found via genemark, 10.00% done  
blasting  
blasted  
parsing  
Exception in thread "_CouplerThread-7 (stdout)" Traceback (most recent call last):  
  File "/Users/mbsulli/jython/Lib/subprocess.py", line 675, in run  
    self.write_func(buf)  
IOError: java.nio.channels.AsynchronousCloseException  
[Fatal Error] 17_2_corr.blastp.xml:15902:63: XML document structures must start and end within the same entity.  
Retry  
blasting  
blasted  
parsing  
Exception in thread "_CouplerThread-9 (stdout)" Traceback (most recent call last):  
  File "/Users/mbsulli/jython/Lib/subprocess.py", line 675, in run  
    self.write_func(buf)  
IOError: java.nio.channels.ClosedChannelException  
[Fatal Error] 17_2_corr.blastp.xml:15890:30: XML document structures must start and end within the same entity.  
Retry  
blasting  

我不知道我的选择是什么。我认为在解析xml之前没有编写它是对的吗?如果是的话,我能确定是谁呢。在

^{pr2}$

Tags: 文件函数in程序most过程stdoutexception
1条回答
网友
1楼 · 发布于 2024-09-24 16:31:20

我想这个问题是在我从对子进程调用wait切换到使用poll方法时开始的,这样我就可以在进程运行时停止它。由于我已经得到了我所使用的许多数据集的结果,在我不得不再次启动子流程之前,有一段时间很难判断。总之,我的猜测是,当我关闭它时,输出仍然被写入,我的解决方案是切换到管道并自己编写文件。在

def cachedBlast(fileName, blastLocation, database, eValue, query, pipeline, remote = False, force = False):


"""
Performs a blast search using the blastp executable and database in blastLocation on
the query with the eValue. The result is an XML file saved to fileName. If fileName
already exists the search is skipped. If remote is true then the search is done remotely.
"""
  if not os.path.isfile(fileName) or force:
    output = open(fileName, "w")
    command = [blastLocation + "/bin/blastp",
               "-evalue", str(eValue),
               "-outfmt", "5",
               "-query", query]
    if remote:
      command += ["-remote",
                  "-db", database]
    else:
      command += ["-num_threads", str(Runtime.getRuntime().availableProcessors()),
                  "-db", database]
    blastProcess = subprocess.Popen(command,
                                    stdout = subprocess.PIPE)
    while blastProcess.poll() == None:
      output.write(blastProcess.stdout.read())
      if pipeline.exception:
        psProcess = subprocess.Popen(["ps", "aux"], stdout = subprocess.PIPE)
        awkProcess = subprocess.Popen(["awk", "/" + " ".join(command).replace("/", "\\/") + "/"], stdin = psProcess.stdout, stdout = subprocess.PIPE)
        for line in awkProcess.stdout:
          subprocess.Popen(["kill", "-9", re.split(r"\s+", line)[1]])
        output.close()
        raise pipeline.exception
    remaining = blastProcess.stdout.read()
    while remaining:
      output.write(remaining)
      remaining = blastProcess.stdout.read()

    output.close()

  try:
    return parseBlast(fileName)
  except SAXParseException:
    return cachedBlast(fileName, blastLocation, database, eValue, query, pipeline, remote, True)

相关问题 更多 >