If you want to go a bit more abstract, timing and event drive programming is what the Twisted python framework is about.
An example of a function acting every second would be:
from twisted.internet import task
from twisted.internet import reactor
def runEverySecond():
print "a second has passed"
l = task.LoopingCall(runEverySecond)
l.start(1.0) # call every second
# l.stop() will stop the looping calls
reactor.run()
This code comes straight from one of the twisted task scheduling examples.
What is particularly cool about this framework is that its completely asleep (no CPU) between the times it wakes up (waking up from timers and events are all handled in the kernal via epoll() or select() not inside the python).
I've been using Twisted heavily for my Rasp Pi dev and I'm quite taken by its ability to handle complex event tasks with very little CPU. Be warned thought that once you get beyond simple tasks it becomes complex fast (... though I would argue complex in a good way).
If you want very comprehensive (and fairly easy to follow) deep dive into it, look at krondo's Twisted Introduction
(BTW Twisted isn't a default framework on raspbian, you need to load it, and to do that you need the python C dev stuff loaded too, so a sudo apt-get install python-dev, sudo apt-get install build-essential followed by a pip install twisted (there is an apt-get for twisted too (python-twisted) but is really old, so I would recommend pip'ing it instead))
If you want to go a bit more abstract, timing and event drive programming is what the Twisted python framework is about.
An example of a function acting every second would be:
from twisted.internet import task
from twisted.internet import reactor
def runEverySecond():
print "a second has passed"
l = task.LoopingCall(runEverySecond)
l.start(1.0) # call every second
# l.stop() will stop the looping calls
reactor.run()
This code comes straight from one of the twisted task scheduling examples.
What is particularly cool about this framework is that its completely asleep (no CPU) between the times it wakes up (waking up from timers and events are all handled in the kernal via epoll() or select() not inside the python).
I've been using Twisted heavily for my Rasp Pi dev and I'm quite taken by its ability to handle complex event tasks with very little CPU. Be warned thought that once you get beyond simple tasks it becomes complex fast (... though I would argue complex in a good way).
If you want very comprehensive (and fairly easy to follow) deep dive into it, look at krondo's Twisted Introduction
(BTW Twisted isn't a default framework on raspbian, you need to load it, and to do that you need the python C dev stuff loaded too, so a sudo apt-get install python-dev, sudo apt-get install build-essential followed by a pip install twisted (there is an apt-get for twisted too (python-twisted) but is really old, so I would recommend pip'ing it instead))
signal.settimer() seems like what you're looking for. please, tell us if for some reason this won't work for you, we'll dig deeper.
Python periodic timer interrupt - Stack Overflow
Timer Interrupt with Python 3 - Raspberry Pi Forums
raspberry pi python gpio timer - Stack Overflow
Timer interrupts python - Raspberry Pi Forums
Videos
Generally the better way to do it would be to create interrupt functions for the rising and falling events. what you're doing now is referred to as busy waiting while polling for an input. Interrupts are generally cleaner and more reliable. Computerphile has a nice overview on interrupts in general (more from the computer aspect of things), and a quick google search found this tutorial on how to use gpio interrupts with the rasberry-pi.
I'd recommend looking at this post (Raspberry Pi- GPIO Events in Python) on the Raspberry Pi SO. That solution shows how to use events so you don't have to run a constant loop - it will just notify you when there is a change.