Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5844501
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:18:04+00:00 2026-05-22T12:18:04+00:00

I’ve got a function in Jython, this function uses Popen to run another program

  • 0

I’ve got a function in Jython, this function uses Popen to run another program which writes an xml file to it’s stdout, which is directed to a file. When the process is done I close the file and call another function to parse it. I’ve been getting a bunch of error messages referring to access to closed files and/or improperly formatted xml files(which appear fine when I look at them) during the parsing. I thought that output.close() may return before closing the file and so I added a loop that waited for output.closed to be true. That seemed to work at first but then my program printed the following

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  

I’m not sure what my options are from here. Was I right to think that the xml is not written before I parsed it? If so who can I do make sure it is.

def parseBlast(fileName):
  """
  A function for parsing XML blast output.
  """
  print "parsing"
  reader = XMLReaderFactory.createXMLReader()
  reader.entityResolver = reader.contentHandler = BlastHandler()
  reader.parse(fileName)
  print "parsed"

  return dict(map(lambda iteration: (iteration.query, iteration), reader.getContentHandler().iterations))

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]
    print "blasting"
    blastProcess = subprocess.Popen(command,
                                      stdout = output)
    while blastProcess.poll() == None:
      if pipeline.exception:
        print "Stopping in blast"
        blastProcess.kill()
        output.close()
        raise pipeline.exception
    output.close()
    while not output.closed:
      pass
    print "blasted"
  try:
    return parseBlast(fileName)
  except SAXParseException:
    print 'Retry'
    return cachedBlast(fileName, blastLocation, database, eValue, query, pipeline, remote, True)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T12:18:05+00:00Added an answer on May 22, 2026 at 12:18 pm

    I think this problem started when I switched from calling wait on the subprocess to using the poll method so I can stop the process while it’s running. Since I already had the results for many of the datasets I worked with it was a while before I had to start the subprocess again so it was hard to tell. Anyway, my guess is that output was still being written into when I closed it and my solution was to switch to pipes and write the file myself.

    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)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i got an object with contents of html markup in it, for example: string
I've got a string that has curly quotes in it. I'd like to replace
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
I want to count how many characters a certain string has in PHP, but
I have a JSP page retrieving data and when single or double quotes are
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.