InteractiveConsole executed in another thread.

Germán Diago germandiago at gmail.com
Tue Oct 20 12:02:29 CEST 2009


Hello. I have an application written in python (it's a wrapper for a
c++ application).

The application has : a window (an SDL window), and a GUI, which has
the window embedded inside.

I would like to use InteractiveConsole to send commands to the SDL window.

I have an API that allows me to do this, but when trying to put
InteractiveConsole in another thread,
my program gets blocked at that point.

I mean:

class FileCacher:
    "Cache the stdout text so we can analyze it before returning it"
    def __init__(self): self.reset()

    def reset(self): self.out = []

    def write(self,line): self.out.append(line)

    def flush(self):
        output = '\n'.join(self.out)
        self.reset()
        return output


class Shell(InteractiveConsole):
    "Wrapper around Python that can filter input/output to the shell"
    def __init__(self):
        self.stdout = sys.stdout
        self.cache = FileCacher()
        InteractiveConsole.__init__(self)
        return

    def get_output(self): sys.stdout = self.cache
    def return_output(self): sys.stdout = self.stdout

    def push(self,line):
        self.get_output()
        # you can filter input here by doing something like
        # line = filter(line)
        InteractiveConsole.push(self,line)
        self.return_output()
        output = self.cache.flush()
        # you can filter the output here by doing something like
        # output = filter(output)
        print output # or do something else with it
        return

class ShellThread(threading.Thread):
     def run(self):
         sh = Shell()
         sh.interact()

The program blocks at interact as if there were no more threads, and
there are 2 python
threads in the program (and one native thread, which releases the GIL
correctly from c++).

Any hints why the Console thread blocks the other one? Thanks in advance.



More information about the Python-list mailing list