Python thread fails to time out when terminal command requires user input

27 views Asked by At

The Task

I'm building a Python script, the purpose of which is to audit a number of .tex files, and one step in this auditing process is to test whether each file will compile, each file being compiled using the terminal command xelatex filename.tex.

These are the methods with which I'm testing whether a given file compiles:

def run_xelatex(self):
    """ Ronseal. """
    self.latex_process = Popen(["xelatex", "current.tex"], stdout=PIPE)
    lines = self.latex_process.stdout.readlines()
    for line in self.latex_process.stdout:
        self.screentext = self.screentext+line.decode("utf-8")+"\n"

def attempt_to_compile(self):
    """ Attempt to compile an article, and kill the process if
    necessary. """
    thread = Thread(target=self.run_xelatex())
    thread.start()
    thread.join(3)
    if thread.is_alive():
        self.latex_process.kill()
        thread.join()
        return False
    return True

In English: I create a thread, which in turn creates a process, which in turn tries to compile a given file. If the thread times out, then that file is marked as being uncompilable.

The Problem

The problem is that, if xelatex finds some bad syntax, it asks the user for manual input in order to resolve the issue. But then, for some reason, the thread does not time out when the process is waiting for user input. This means that, when I try to run the script, it stops in mid-flow at several points, until I mash the return key to get things going again. This is not ideal.

What I Want

  • An explanation of why a thread fails to time out when a process within it asks for user input.
  • A solution to the problem, either by forcing the thread to time out in the above circumstances, or by preventing xelatex from asking for user input.
  • Alternatively, an explanation for why what I'm trying to achieve is totally insane, and a suggestion for a better line of attack.
0

There are 0 answers