Python seems to have crashed - code optimization?
Hello everyone,
I'm currently trying to set up an EEG experiment where we show a series of images while at the same time measuring peoples breathing phase.
That means that while we are showing images we want to listen at the same time to our parallel ports to see if an inhalation or exhalation has been detected, and if see to send a marker to our EEG data.
I managed to write a code that sort of does this however, the experiment crashes often. Therefore I wanted to ask if anyone has some tips for code optimization. I run the experiment on windows 7 with the legacy back-end. I'm grateful for any thoughts or comments!
Here are the two core pieces of inline script:
- Prepare script at the beginning of the experiment sequence
#respiration state check var.state = u'neutral' # look for in/exhalation and respond accordingly def checkPorts(): global io dev = windll.inpout32 val = dev.Inp32(PLport) #detect inhalation if val == 8 & (var.state == u'neutral' or var.state == u'exhalation'): #send trigger io.Out32(EEGport,10) print 'Send trigger 10 to EEGport' #change respiration state var.state = u'inhalation' #reset PLport io.Out32(PLport, 0) print 'Send trigger 0 to PLport' clock.sleep(195) #detect exhalation elif val == 2 & (var.state == u'neutral' or var.state == u'inhalation'): #send trigger io.Out32(EEGport,20) print 'Send trigger 20 to EEGport' #change respiration state var.state = u'exhalation' #reset PLport io.Out32(PLport, 0) print 'Send trigger 0 to PLport' clock.sleep(195)
2. Run script of one of my blocks
# save timepoint t0 = clock.time() #show fixation cross for 1 sec my_ITI.show() # Check for in/exhalation while clock.time()-t0 < 995: checkPorts() #show image for duration length my_photo.show() # Check for in/exhalation while clock.time()-t0 < var.duration+995: checkPorts() #show fixation cross for 0.5 sec my_ITI.show() # Check for in/exhalation while clock.time()-t0 < var.duration+1495: checkPorts()
Comments
I now included a 10ms timeout after each time the checkPorts() function is called to decrease the CPU load, but it still keeps crashing.
I managed to stop it from crashing and doing what I want by increasing the timeout to 15 ms and rewriting the if statements so that they are more clear.