Fatal Python error: (pygame parachute) Bus Error

Fatal Python error: (pygame parachute) Bus Error

这是我的代码:

from psychopy import visual, event, gui, data, core
import random, os
from random import shuffle
from PIL import Image
import glob

a = glob.glob("DDtest/targetimagelist1/*")
b = glob.glob("DDtest/distractorimagelist1/*")
c = glob.glob("DDtest/targetimagelist2/*")
d = glob.glob("DDtest/distractorimagelist3/*")
e = glob.glob("DDtest/targetimagelist4/*")

shuffle(c)
shuffle(d)

ac = a + c
bd = b + d

indices = random.sample(range(len(ac)),len(ac))
ac = list(map(ac.__getitem__, indices))
bd = list(map(bd.__getitem__, indices))

ace = ac+e
shuffle(ace)

target = ac
distractor = bd 
recognition = ace


def studyphase():


    loc = [1, 2]
    location = random.choice(loc)
    if location == 1:
        pos1 = [-.05,-.05]
        pos2 = [.05, .05]
    else:
        pos1 = [.05, .05]
        pos2 = [-.05, -.05]


    win = visual.Window(size=(1920, 1080), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1])
    distractorstim = visual.ImageStim(win=win, pos=pos1, size=[0.5,0.5])
    distractorstim.autoDraw = True
    targetstim = visual.ImageStim(win=win, pos=pos2, size=[0.5,0.5])
    targetstim.autoDraw = True

    targetstim.image = target[i]
    distractorstim.image = distractor[i]


    win.flip()
    core.wait(.1)

def testphase():

    win = visual.Window(size=(1920, 1080 ), fullscr=True, screen=0, monitor='testMonitor', color=[-1,-1,-1])
    recognitionstim = visual.ImageStim(win=win, pos=[0,0], size=[0.5,0.5])
    recognitionstim.autoDraw = True



    recognitionstim.image = recognition[k]

    old = visual.TextStim(win,text='OLD',pos=[-0.5,-0.5],font='Lucida Console')
    new = visual.TextStim(win,text='NEW', pos=[0.5,-0.5],font='Lucida Console')

    old.draw()
    new.draw()
    win.flip()
    core.wait(.1)


for i in range(len(ac)):
    studyphase()

for k in range(len(ace)):
    testphase()

这应该做的是拍摄一堆照片并在两个不同的阶段(研究和测试)显示它们,但是,当我 运行 这段代码时,程序在第二个阶段的一半左右崩溃了循环,我收到以下错误消息:

python(55762,0xa0afe1a8) malloc: *** mach_vm_map(size=8388608) failed (error code=3)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug
Fatal Python error: (pygame parachute) Bus Error

但是,如果我 运行 独立地研究循环或测试循环,它们 运行 没问题。任何人都知道可能导致此错误的原因是什么?任何帮助将不胜感激。 :)

编辑:显然,如果我将 win 命令移到循环之外,它就可以工作。

这个问题是 raised on the psychopy-users list 几年前的事了。这很可能是由于图像太大(以像素为单位,而不是兆字节)造成的。因此,如果可能的话,一个解决方案是将它们缩小到大约您要显示它们的分辨率。我通过谷歌搜索错误消息找到了这个。

您在每个 trial/presentation 上生成一个新的 window 和几个新的刺激,因为它们是在函数内启动的,并且在循环的每次迭代中都会调用函数。请 获取先创建 window/stimuli 的策略,然后更新需要更改的属性。这甚至可以自行解决问题,因为创建新的刺激可能会填满内存。