~chromakode/boots/nodes-async

« back to all changes in this revision

Viewing changes to src/boots/lib/lingos/external.py

  • Committer: David Rosenbaum
  • Date: 2010-02-08 19:08:53 UTC
  • mfrom: (56.1.1 boots)
  • Revision ID: davidjrosenbaum@comcast.net-20100208190853-3hqambih3blnm37z
Merge in updated external lingo code.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
    def __init__(self, executable, executable_args, *args, **kwargs):
39
39
        """Create a new interpreter using the specified executable with the specified arguments."""
40
40
        super(ExternalInterpreter, self).__init__(*args, **kwargs)
41
 
        self.interpreter = subprocess.Popen(executable, *executable_args, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
42
 
        # Since self.interpreter.stdout.readline could block, a thread is used to read the output.
43
 
        self.read_thread = threading.Thread(target = self._readlines)
44
 
        self.read_thread.start()
45
 
        self.output_lines = []
46
 
        self.lock = threading.RLock()
 
41
        self.executable = executable
 
42
        self.executable_args = executable_args
 
43
        self.interpreter = None
47
44
 
48
45
    def _readlines(self):
49
46
        while True:
55
52
    
56
53
    def evaluate(self, code):
57
54
        """Evaluate code using the external interpreter.  All return values are strings."""
58
 
        # The maximum amount of time to wait for output.
59
 
        # FIXME: This is a kludge and should be fixed if possible.
 
55
        if self.interpreter == None:
 
56
            self.interpreter = subprocess.Popen(self.executable, *self.executable_args, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
 
57
            # Since self.interpreter.stdout.readline could block, a thread is used to read the output.
 
58
            self.read_thread = threading.Thread(target = self._readlines)
 
59
            self.read_thread.daemon = True
 
60
            self.read_thread.start()
 
61
            self.output_lines = []
 
62
            self.lock = threading.RLock()
 
63
            # The maximum amount of time to wait for output.
 
64
            # FIXME: This is a kludge and should be fixed if possible.
 
65
 
60
66
        read_wait = 0.1
61
67
        
62
68
        with self.lock: