Reputation: 689
I am working on an animal tracking program with OpenCV. When the animal is recognised, I want to draw some shapes and project them in front of it for a certain number of seconds. For drawing the shapes and projecting them I am using PsychoPy
When I get the animal and call the draw function with PsychoPy, the program freezes until the shape is disappeared. I used the Threading to solve this, but the program stops completely with a system message says "Python quit unexpectedly."
Here is how I am opening the thread:
t = threading.Thread(target=stimulus_controller.draw, args=(stimulus_view, 20))
t.setDaemon(True)
t.start()
where the stimulus_view is an array of the shapes I want to draw, and the 20 is the number of seconds to show the stimulus.
And this is the drawing code:
def draw(stims, time):
trialClock = core.Clock()
while t < time:
t = trialClock.getTime()
for s in stims:
s.draw()
myWin.flip()
It is simple but it keeps stopping unexpectedly!
Thank you very much.
Upvotes: 3
Views: 145
Reputation:
In general, windowing toolkits do not like being used from different threads (this is mostly due to the way the underyling OS works), and PsychoPy is no exception. It is not always documented, but very often it is possible to use them with threads, provided a single thread (be it the main thread or a worker thread) interacts with the window.
"Interacting" with the window is essentially three things: creating it, drawing into it, and sending/receiving events to/from it. In your example, you draw from the worker thread, but you have created the window from the main thread - hence it crashes.
If you create the window from within your worker thread, you should be fine. Also, if you intend to use events, do this only from the worker (some toolkits, e.g. wxWidgets, have primitives to allow other threads to send events, some require you to do the plumbing using e.g. a queue).
Upvotes: 3