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.
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! ;-)