PsychoPy:通过子流程在主流程和时间中显示

PsychoPy : Displaying in main process and Time through a subprocess

我使用的是实验室以前的博士生完成的心理代码。此代码旨在显示刺激(随机点运动图)并使用子进程进行精确计时。

子流程是使用以下行创建的:

process = subprocess.Popen(['python', 'LPTmat.py'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)

然后在显示第一帧的时候,代码写一个数字告诉子进程开始计时:

if first_frame == True:
        process.stdin.write('%i\n'%1) #start timer and check of the PP
        first_frame = False

子进程然后启动定时器并记录按下的按钮(并口):

while True:
input = sys.stdin.readline()
if input == '1\n':
    timer.reset()
    parallel_port_state = ctypes.windll.inpout32.Inp32(lptno)       
    while parallel_port_state == etat_repos:
        parallel_port_state = ctypes.windll.inpout32.Inp32(lptno)                  
        lecture_time = timer.getTime()
    if  parallel_port_state in port_gauche:
        send_trigger (1)
    elif  parallel_port_state in port_droit:
        send_trigger (2)
    np.savetxt('mat.txt',mat)#a button has been pressed   

检测 txt 文件并停止刺激呈现的主要进程:

    mtext= os.path.exists('mat.txt')
    if mtext== True:#if the mat.txt file exists, a button has been pressed 
        myWin.flip()#black screen
        process.stdin.write('%i\n'%3)
        check = process.stdout.readline()#read which button has been pressed
        check = int(check)

然后通过子进程检查响应时间记录器并删除创建的 txt 文件:

process.stdin.write('%i\n'%2)
RT = process.stdout.readline()
RT = float (RT)
rep.rt = RT
os.remove('mat.txt')

问题是创建的 .txt 文件并不是完成这项工作的真正干净方式,所以我想知道它们是否是使用此子进程并告诉主进程已做出响应的另一种方式?

干杯, 加布里埃尔

异步设备轮询是导致 ioHub 开发的主要原因之一,它已于去年左右合并到 PsychoPy 中。本质上,ioHub 创建了一个新的 Python 进程,该进程 与您的外部设备连接,而主 PsychoPy 进程可以继续呈现刺激,不受设备轮询的干扰。在任何需要的时间,例如在刺激生成和呈现的时间关键阶段已经过去之后,PsychoPy 进程可以从 ioHub 进程请求收集的数据。

(另请注意,普通的 Python thread 永远不会在 CPython 中同时执行 really由于 GIL;这就是为什么 ioHub 创建了一个全新的 进程, 而不仅仅是另一个线程。)

ioHub 已经支持许多不同类型的硬件和接口(串行端口、眼动仪、不同类型的响应按钮盒等)。不幸的是,据我所知,迄今为止还没有集成并行端口支持。

但不要绝望!我看到 已经支持 LabJack devices, which have replaced the steadily disappearing parallel port in many psychophysics and electrophysiology labs. These devices are relatively cheap (about USD 300) and can be connected to modern computers via a USB port. ioHub has a ready-to-use interface for the LabJacks, which is also demonstrated in this demo.

当然,另一种选择是为 ioHub 开发您自己的并行端口接口。但鉴于此界面的流行度、可用性和适用性正在消失,我想知道这是否真的值得付出努力。