Anything similar to a microcontroller interrupt handler?
<p>Is there some method where one could use a try statement to catch an error caused by a raise statement, execute code to handle the flag e.g. update some variables and then return to the line where the code had been operating when the flag was raised?</p>

<p>I am thinking specifically of an interrupt handler for a micro-controller (which does what ive just described).</p>

<p>I am writing some code that has a thread checking a file to see if it updates and I want it to interrupt the main program so it is aware of the update, deals with it appropriately, and returns to the line it was running when interrupted. </p>

<p>Ideally, the main program would recognize the flag from the thread regardless of where it is in execution. A try statement would do this but how could I return to the line where the flag was raised?</p>

<p>Thanks!
Paul</p>

<p>EDIT:</p>

<p>My attempt at ISR after comments albeit it looks like a pretty straight forward example of using locks. Small test routine at the bottom to demonstrate code</p>

<pre><code>    import os
    import threading
    import time

    def isr(path, interrupt):
        prev_mod = os.stat(path).st_mtime
        while(1):  
            new_mod = os.stat(path).st_mtime
            if new_mod != prev_mod:
                print "Updates! Waiting to begin"
                # Prevent enter into critical code and updating
                # While the critical code is running.
                with interrupt:     
                    print "Starting updates"
                    prev_mod = new_mod
                    print "Fished updating"
            else:
                print "No updates"
                time.sleep(1)

    def func2(interrupt): 
        while(1):
            with interrupt:     # Prevent updates while running critical code
                # Execute critical code
                print "Running Crit Code"
                time.sleep(5)
                print "Finished Crit Code"
            # Do other things

    interrupt = threading.Lock()

    path = "testfil.txt"
    t1 = threading.Thread(target = isr, args = (path, interrupt))
    t2 = threading.Thread(target = func2, args = (interrupt,))
    t1.start()
    t2.start()

    # Create and "Update" to the file
    time.sleep(12)
    chngfile = open("testfil.txt","w")
    chngfile.write("changing the file")
    chngfile.close()
    time.sleep(10)
</code></pre>
