Running Psychopy window from a thread segfaults
Question: I'm trying to run my Psychopy window from a separate thread and control what's
shown on it from another one, but all I get is Fatal Python error.

Here's a small example script that produces the same results as my larger one

    
    
    from threading import Thread
    from psychopy import visual, core
    import time
    
    class ThreadTest(Thread):
        def __init__(self):
            Thread.__init__(self)
            self.text='Test'
            self.running = 1
            self.start()
            print 'doing stuff'
    
        def run(self):
            win = visual.Window()
    
            msg = visual.TextStim(win, text=self.text)
            while self.running:
                msg.setText(self.text)
                msg.draw()
                win.flip()
                print 'Drawing...'
                core.wait(2)
    
            win.close()
            print 'Stopping thread'
    
        def setText(self, text):
            self.text=text
    
        def stopTest(self):
            self.running = 0
    
    
    def main():
        tt = ThreadTest()
        time.sleep(3)
        tt.setText('Test2')
        time.sleep(3)
        tt.stopTest()
        print 'Stopping main thread'
    
    if __name__ == '__main__':
        main()
    

and the output

    
    
    python testy.py 
    doing stuff
    Fatal Python error: (pygame parachute) Segmentation Fault
    Aborted (core dumped)
    

This creates the Psychopy window but fails to show any te xt on it and then
just crashes. I've also tried creating the window in `__init__()` but that
didn't work either.


Answer: This seems to be an issue with the text object, which come from pyglet and
might interfere with pyglet threading. The code below actually works (to my
amazement)!

BUT you're doing something that's strongly discouraged and not supported.
OpenGL calls (that handle all the rendering) are not thread safe and shouldn't
be called from anything other than the main thread.

Basically, you're on your own from here! ;-)

    
    
    from threading import Thread
    from psychopy import visual, core
    import time
    
    class ThreadTest(Thread):
        def __init__(self):
            Thread.__init__(self)
            self.ori=0
            self.running = 1
            print 'initialised'
    
        def run(self):
            win = visual.Window()
            print 'created window'
            stim = visual.PatchStim(win)
            print 'created stim'
            while self.running:
                stim.ori = self.ori
                stim.draw()
                win.flip()
                print '.',
                core.wait(0.01)
    
            print 'Stopping auxil thread'
    
        def setOri(self, ori):
            self.ori=ori
    
        def stopTest(self):
            self.running = 0
    
    
    def main():
        tt = ThreadTest()
        tt.start()
        for frameN in range(180):
            tt.setOri(frameN)
            time.sleep(0.01)
        tt.stopTest()
        print 'Stopping main thread'
    
    if __name__ == '__main__':
        main()
    

