Q:

Several issues with capturing a screen video with python

i have already asked a similar question on stackoverflow, but its not the same :-)
I have three problems with my code:

The framerate is not steady. When i execute the code without the screengrab, i still get low framerates, and whats worse, the framerate varies. I post output below. Any tips to fix this?
Its very slow. What this code will do is check for a pixel on the screen and record the time leading up to the occurrence of said pixel. Since there are potentially long pauses between records, i want to include a buffer that gets written in a worker thread, have not figured this out yet. Will this affect performance?
Sometimes it can happen that the pixel comes quickly after the first record. How do i prevent frameratedrops and how do i merge it with the other frames?

So here is my code: 
import PIL.ImageGrab
import time

def get_pixel_colour(i_x, i_y):

    return PIL.ImageGrab.grab().load()[i_x, i_y]

print get_pixel_colour(0, 0)
fps = 5
skipticks = 1/(fps*1.0)
i= 0
allframes=[]
nextsnap=time.clock()
print skipticks, fps
while (True):
    tim= time.clock()
    i=i+1
    #x = PIL.ImageGrab.grab()
    # this prints the fps
    #'print 'Fps at start',i, 1/(time.time()-tim)
    #   x.save("tm\screengrab"+ str(i) +".jpg")
    # this is the sleep that limits the fps
    nextsnap+=skipticks
    # print nextsnap, skipticks
    sleeptime = nextsnap-time.clock()
    #print "want to sleep",sleeptime
    if (sleeptime>0):
        time.sleep (sleeptime)
    else:
        print 'took too long'
    print 'Fps at end:#', i, 1/(time.clock()-tim)

here is my output, when the screengrab is commented out:
Fps at end:# 1 4.93170511268
Fps at end:# 2 5.0171558081
Fps at end:# 3 5.43777363226
Fps at end:# 4 5.00931318904
Fps at end:# 5 5.00381624434
Fps at end:# 6 5.01183110503
Fps at end:# 7 5.01101022488
Fps at end:# 8 5.43623758467
Fps at end:# 9 5.04215239941

Here when it is not commented out:
Fps at end:# 13 5.05128838338
Fps at end:# 14 5.07528828608
Fps at end:# 15 5.51303968815
Fps at end:# 16 5.05357755475
Fps at end:# 17 5.0671482485
Fps at end:# 18 5.06788717279
Fps at end:# 19 5.06737062052
Fps at end:# 20 5.48886217371
Fps at end:# 21 5.067822598

i hope someone is able to help here :-)

A:

Alrighty, after looking around for a while, I've gotten these links together. First, this is similar to writing a refresh screen loop in a game. Read this for some of the pitfalls with sleeping. For a better solution, you really want to have your program wait until the kernel updates the display, which should be a kernel event in linux. You'd do this by setting up a event queue with the select library. 
The only name I could find for the event queue to monitor was here, with fbdev_evtchn as the channel. This may not apply. 
If you find yourself on windows, or can't get notified of screen refreshes, then both the select library and the signal library have real-time timers. Those will give you an accurate wait, and depending on the implementation, should put the program under until the timer runs out.

