Q:

Read file continuously and update Python GTK application accordingly

I am working on a gesture recognition project. There is a C program that captures the gestures from video input and write them to the file. Now, I have an image viewer written in Python - GTK. It switches to the next or previous window according to gestures. This continuous file read and application update is defined as follows:
def ai(self):
        if self.mtime!=os.stat("input.txt").st_mtime:
            self.mtime=os.stat("input.txt").st_mtime
            inp=open("input.txt", "r");
            line=inp.read()
            if len(line)!=0:
                line=line[0:len(line)-1]
                for x in line.split(' '):
                    y=x.split('-')
                    if(y[1]=="R"):
                        self.next()
                        print "Move Right"
                    elif(y[1]=="L"):
                        self.prev()
                        print "Move Left"
        time.sleep(1)
        print "WakeUp"
        self.ai()

Now if I comment out the recursive call here, then application works after reading present file and updating images. Of course, then it doesn't update afterwards. 
But if I uncomment the recursive call, then the console keeps on working but image-viewer application stops responding. 
Is there any way to perform this function? Any event where i could bind this ai() function that could check for file updates every time that event is fired...
Any help would be appreciated.

A:

Not sure if I understand you correctly, but I would expect what you want to do is to schedule a call to ai() in your main GTK loop, so that your program checks if there is input on a regular base.
If this is what you want you have two choices: scheduling the call periodically or schedule the call for when the program is idle (it is not doing anything else).
Your code should look something like:
gobject.idle_add(callback, ...)  #execute callback when idle
gobject.timeout_add(interval, callback, ...)  #execute callback every interval millisecs

The documentation should be here but presently there is a server error. Here are the relevant passages:

The gobject.idle_add() function adds a function (specified by callback) to be called whenever there are no higher priority events pending to the default main loop. The function is given the default idle priority, gobject.PRIORITY_DEFAULT_IDLE. Additional arguments to pass to callback can be specified after callback. The idle priority can be specified as a keyword-value pair with the keyword "priority". If callback returns FALSE it is automatically removed from the list of event sources and will not be called again.
The gobject.timeout_add() function sets a function (specified by callback) to be called at regular intervals (specified by interval, with the default priority, gobject.PRIORITY_DEFAULT. Additional arguments to pass to callback can be specified after callback. The idle priority may be specified as a keyword-value pair with the keyword "priority".

A catch: you have your callbacks to return True if you want to keep them in the scheduler, failing to do so will make them execute only once.
HTH!

