It looks from the graph as if that glitch is a single missed frame, right?
Here is some stuff you could try:
1 - You could reject the experimental trials with missed frames instead of eliminating the missed frames. If there is no correlation between the frame miss and the stimulus then rejecting glitch trials will not introduce bias. If the glitches are infrequent and each presentation/trial short, then throwing out trials with timing glitches would be cheap. For the opposite extremes, rejecting trials which contain missed frames would not be practical because too high a proportion would. In the worst case, no trials would have missed frames.
One common situation in which you are likely to see a correlation between the stimulus and the glitch is if your trials vary in duration, because longer trials are more likely to contain glitches than shorter trials. In that case, to preserve randomization and balanced presentation ratios, over-collect data for all stimuli, then reject as many short trials as long trials.
2 - Raise the scheduling priority of your Python process during the timing test. Not really sure how to do that in Psychopy/Python, looks like it had been in psychopy.core but I can’t find it there now. Someone else around here, certainly Jon but probably others, would know what is going on with that. Whatever the API in Python, it will resolve to the system API for setting priority, which on Linux and OS X have settings for real-time task scheduling. You want those. Also, a Linux OS can be configured to run real-time, low-latency and normal kernels.
3 - Timing can vary by OS, try Linux and/or OS X. Per previous point: particularly in conjunction with setting task scheduling priority, because that works differently (and better) in OS X and Linux.
4 - In case that missed frame is caused by the Python interpreter allocating or freeing memory during the test, make sure memory use in your script is constant during the timing loop. In MATLAB I create a matrix before the timing loop and then during the loop replace elements in the matrix with timestamps. There might be something similar which you could do in Python: I would expect that a numpy matrix is a monolithic block of memory and its elements resolve to a C types within instead of to dynamically allocated objects per-element. Python lists, on the other hand, certainly would allocate and free python objects (and therefore memory) as elements are added or replaced.
5- The suggestion to close all other applications during the timing test was a good one.
6- I don’t know if PyPy works with PsychoPy but, if so, then you might see different and better results with that. PyPy is much faster than C Python. Faster is not the same as real-time, but but nonetheless might buy you enough to meet the blanking deadline more reliably.
7- If it were me, I would do everything I could to make it work as well as I could in Psychopy/Python. Then, if I absolutely had to have better timing I would go look at the Psychtoolbox with whatever is the recommended best graphics card and driver for that currently and check if that is any better than what I could get with Psychopy/Python. If that reduces the glitches, then either just use that, or figure out why and copy back the solution to Psychopy.
8- Jon has documentation on display timing here. Someone could look into that constant memory issue (#4 above) to see if it is the cause of the glitches and if Psychopy is allocating/freeing memory to record refresh interval timestamps; Its possible you could get a negative result if you eliminate memory allocations/frees in your test script because Psychopy is allocating/freeing under the hood.
Because there are many possible causes of timing glitches, I am not confident which of those suggestions will help. Therefore, for each I would do comparative testing with enough run time to identify only statistically significant differences between conditions. Maybe someone else has already explored all of that and can tell you what usually works best.
Achieving reliable timing is broadly useful for psychophysics stimulus displays so that documenting and reporting back favorable test results here would win you a lot of positive Karma.
The OS+ Python interpreter are optimized for speed, not for reliably meeting timing deadlines. If simpler solutions do not work then you would be left with the very hard work of messing with the internals of the OS and the Python interpreter and video driver.
best,
Allen