I'm using a psychopy code that was done by a previous Phd student of the lab. This code aims to display stimuli (random dot kinematogram) and use a subprocess for precise timing.
The subprocess is created with the following line :
process = subprocess.Popen(['python', 'LPTmat.py'], shell=False, stdin=subprocess.PIPE, stdout=subprocess.PIPE)
Then when the first frame is displayed, the code writes a number to tell the subprocess to begin timing:
if first_frame == True:
process.stdin.write('%i\n'%1) #start timer and check of the PP
first_frame = False
The Subprocess then start the timer and record the button pressed (parallel port) :
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
The main process than detect the txt file and stop the stimulus presentation :
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)
And go checking back the response time recorder by the subprocess and remove the txt file created :
process.stdin.write('%i\n'%2)
RT = process.stdout.readline()
RT = float (RT)
rep.rt = RT
os.remove('mat.txt')
The problem is that the .txt file created is not really a clean way to do the job so I was wondering if they were another way to use this subprocess and to tell the main process that a response was made ?
Cheers, Gabriel
Asynchronous device polling was one of the main reasons that lead to the development of ioHub, which has been merged into PsychoPy last year or so. Essentially, ioHub creates a new Python process that only interfaces with your external devices, while the main PsychoPy process can continue to present stimuli, uninterrupted by device polling. At any desired time, for example after time-critical phases of stimulus generation and presentation have passed, the PsychoPy process can request the collected data from the ioHub process.
(Please also note that an ordinary Python thread is never executed really simultaneously in CPython due to the GIL; that is why ioHub creates a whole new process, not just another thread.)
ioHub already supports many different types of hardware and interfaces (serial port, eye trackers, different types of response button boxes etc.) Unfortunately, to my knowledge no parallel port support has been integrated to date.
But do not despair! I see there is already support for 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.
Another alternative, of course, would be to develop your own parallel port interface for ioHub. But given the vanishing popularity, availability, and therefore applicability of this interface, I am wondering whether this is really worth the effort.