psychopy.org | Reference | Downloads | Github

Memory error when for-loop contains too many videos (python, psychopy function visual.MovieStim3)

Hello everybody,

I created a loop in Psychopy 1.85.2 that shows a video in each iteration. For a few iterations, it seems to work fine. However, when I include all videos (106), I run into memory error problems each time.

The code is Python. The video object is created with the Psychopy function ‘visual.MovieStim3’. I am not entirely sure about the video objects being the issue here. But because I thought they were, I tried to delete the video objects after their presentation using the following snippets, but it did not work:

del video._mov
video._unload()
win.clearBuffer()

This is the video snippet from within the loop:

# play the video
video = visual.MovieStim3(win, filename = trialArray[trial], size=(1920,1200), flipVert=False, flipHoriz=False, loop=False, opacity=(1.0))
current_time = trial_clock.getTime()
while trial_clock.getTime() < current_time + 6.6:
    video.draw()
    win.flip()
    # start registering potential keypresses
    keypress_video = event.getKeys(keyList=None, modifiers=False, timeStamped=trial_clock)
    if keypress_video != []:
        keypresses.append(keypress_video)
        # include the escape key as a way to exit the experiment. 
        if keypress_video[0][0] == 'escape':
            print ('Sie haben das Experiment verlassen.')
            sys.exit()
        else:
            pass
    else:
        pass
video_duration = trial_clock.getTime() - current_time
video.stop()

I sometimes get this error message: “Fatal Python error: (pygame parachute) Segmentation Fault”

But otherwise, I mostly get this one:

or this: “Traceback (most recent call last):
File “K:\Mitarbeiter\Juliane\1_PaGiPoP\experiment\02_Lab-study\2019-05-17_script-for-lab-study\pagipop_rating_04-test.py”, line 292, in
runTrial()
File “K:\Mitarbeiter\Juliane\1_PaGiPoP\experiment\02_Lab-study\2019-05-17_script-for-lab-study\pagipop_rating_04-test.py”, line 150, in runTrial
video.draw()
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\movie3.py”, line 380, in draw
self._updateFrameTexture() # will check if it’s needed
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\psychopy\visual\movie3.py”, line 305, in _updateFrameTexture
self._numpyFrame = self._mov.get_frame(self._nextFrameT)
File “”, line 2, in get_frame
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\moviepy\decorators.py”, line 89, in wrapper
return f(*new_a, **new_kw)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\moviepy\Clip.py”, line 95, in get_frame
return self.make_frame(t)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\moviepy\video\io\VideoFileClip.py”, line 103, in
self.make_frame = lambda t: self.reader.get_frame(t)
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\moviepy\video\io\ffmpeg_reader.py”, line 179, in get_frame
result = self.read_frame()
File “C:\Program Files (x86)\PsychoPy2\lib\site-packages\moviepy\video\io\ffmpeg_reader.py”, line 147, in read_frame
result = np.fromstring(s, dtype=‘uint8’)
MemoryError
portaudio error in Pa_AbortStream: Wait timed out”

Your PsychoPy release is now over two years old. I suspect most people’s first response to this would be “can you confirm that this is still an active issue?”

I’m not saying it won’t still be an issue, but there would be more enthusiasm for looking into it if it is.

Open the task manager and check whether a new video instance is added to the python.exe once you load the next video. If so, this would indicate that you are not yet successful in unloading the video.

Also check my answer in the following thread for unloading the movie. In particular, also add the ‘del video._mov.reader’ command to your script before the ‘del video._mov’ line:

1 Like

Hey Michael, thank you for your answer. I cannot upgrade to the newer version for current practical reasons in our lab.

Hi Frank,

thank you for your answer.

the idea with the task manager was very helpful, I was not thinking of that.
I do not get the system behind this, but some of the videos do not unload after presentation, so the ffmpeg.exe-processes start to pile up over the duration of the experiment.

I found that if I include the line ‘del video._mov’ into my code, the code crashes and I get the error Message: 'AttributeError: ‘NoneType’ object has no attribute ‘reader’.

But then, I realized that the video.stop() function that I have in my code might actually be the reason the video is kept alive, or anyway, it might not make so much sense (if everything goes right, the video should stop anyway). So I got rid of that and now I can use the code line ‘del video._mov’, AND IT FINALLY WORKS!

:smiley:

Best wishes,
Juliane

1 Like

Hi Juliane,

good to hear that you could solve your issue.

All the best,

Frank.

1 Like

Thanks! Removing the video.stop() function was exactly what I needed.