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))

Answer from Mike Lutz on Stack Exchange
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Timer Interrupt with Python 3 - Raspberry Pi Forums
In my case, i want to check pin states during 1s and then still process in my program. I tried to use this "threading" module and I did this little program : ... import threading def hello(): print('Hello') a=1 t = threading.Timer(5.0, hello) t.start() a=0 while(a==0): print("you're here") print('Good By') The timer method is working, it print a 'Hello' after 5 seconds but unfortunately, as a function,it doesn't save my variable.
Top answer
1 of 2
1

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))

2 of 2
0

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.

Discussions

Python periodic timer interrupt - Stack Overflow
(How) can I activate a periodic timer interrupt in Python? For example there is a main loop and a timer interrupt, which should be triggered periodically: def handler(): # do interrupt stuff ... More on stackoverflow.com
🌐 stackoverflow.com
Timer Interrupt with Python 3 - Raspberry Pi Forums
I'm beginning with Raspberry Pi 3 and I looked on the web but didn't see any post dealing with the timer interrupt on PI 3. I only find stuff about the twited library but it seems to be only available on PI2 and previous version... So my question is : Is it possible to make a timer interrupt with Python ... More on raspberrypi.org
🌐 raspberrypi.org
February 15, 2018
raspberry pi python gpio timer - Stack Overflow
Computerphile has a nice overview ... on how to use gpio interrupts with the rasberry-pi. ... Sign up to request clarification or add additional context in comments. ... I'd recommend looking at this post (Raspberry Pi- GPIO Events in Python) on the Raspberry Pi ... More on stackoverflow.com
🌐 stackoverflow.com
Timer interrupt? Vergleichbares? - Python - Deutsches Raspberry Pi Forum
[/font] [font="Calibri"]Bei meiner bisherigen Suche bin ich auf die Dokumentation des Prozessors des Raspberry Pi gestoßen: „BCM2835-ARM-Peripherals“. Darin ist auch die Rede von einem Interruptfähigen Timer. Doch ich hab keine Ahnung wie ich diesen über ein Python Programm verwenden ... More on forum-raspberrypi.de
🌐 forum-raspberrypi.de
June 9, 2017
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › hardware and peripherals › raspberry pi pico › micropython
Interrupt timer in Micropython? - Raspberry Pi Forums
What they are calling "PWM" cannot be generated by typical PWM hardware. Here we want nothing, one or more pulses pushed out at a particular rate when we need them. Timer interrupts or callbacks can potentially do that, PIO state machines can, PWM hardware cannot.
🌐
The Robotics Back-End
roboticsbackend.com › home › raspberry pi gpio interrupts tutorial
Raspberry Pi GPIO Interrupts Tutorial - The Robotics Back-End
December 30, 2021 - Learn why, when, and how to use interrupts with GPIOs on your Raspberry Pi programs. Complete tutorial with code examples in Python, using RPi.GPIO module.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › community › general discussion
Raspi timer interrupts in python - Raspberry Pi Forums
TL;DR : Use callbacks as "interrupts". ghans · • Don't like the board ? Missing features ? Change to the prosilver theme ! You can find it in your settings. • Don't like to search the forum BEFORE posting 'cos it's useless ? Try googling : yoursearchtermshere site:raspberrypi.org
🌐
Random Nerd Tutorials
randomnerdtutorials.com › home › raspberry pi pico › raspberry pi pico with interrupts: external and timer interrupts (micropython)
Raspberry Pi Pico Interrupts: External and Timer (MicroPython) | Random Nerd Tutorials
December 3, 2025 - The following example makes use of the Timer class to blink an LED every half a second. # Rui Santos & Sara Santos - Random Nerd Tutorials # Complete project details at https://RandomNerdTutorials.com/raspberry-pi-pico-interrupts-micropython/ from machine import Pin, Timer from time import sleep # LED pin led_pin = 20 led = Pin(led_pin, Pin.OUT) # Callback function for the timer def toggle_led(timer): led.value(not led.value()) # Toggle the LED state (ON/OFF) # Create a periodic timer blink_timer = Timer() blink_timer.init(mode=Timer.PERIODIC, period=500, callback=toggle_led) # Timer repeats every half second # Main loop (optional) while True: print('Main Loop is running') sleep(2)
Find elsewhere
🌐
RasPi.TV
raspi.tv › 2013 › how-to-use-interrupts-with-python-on-the-raspberry-pi-and-rpi-gpio-part-3
How to use interrupts with Python on the Raspberry Pi and RPi.GPIO – part 3
June 22, 2015 - In other words, the raspberry pi should be running through its normal routine, and whenever an interval of time has passed (let’s say a second) it would go the interrupt routine. Can you help me? ... This method has only to do with GPIO hardware interrupts. What you appear to want is a second thread running a timer (or a hardware clock that can give you a hardware interrupt). Suggest you look up threads and threading on the Python ...
🌐
Upsy
upesy.com › tutorials › raspberry pi pico › pi pico programming › micropython › basics › timers
Timer Pico with MicroPython: Master the time - uPesy
February 2, 2023 - This simple Python script shows how to use the Pico timer. It can call interruption_handler() at the end of each period set on the timer.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Interrupt at one minute intervals in Python - Raspberry Pi Forums
import threading import time def callback(): global i print("timer interrupt", i) # # more code here... # i += 1 timer = threading.Timer(60.0, callback) # start a timer again timer.start() i = 0 print("start a timer") timer = threading.Timer(60.0, callback) timer.start() while True: print("run...") now = time.localtime() print("{}/{}/{}, {}:{:02}:{:02}".format(now[1], now[2], now[0], now[3], now[4], now[5])) time.sleep(5)
🌐
Raspberry Pi Forum
forum-raspberrypi.de › deutsches raspberry pi forum › programmierung › python
Timer interrupt? Vergleichbares? - Python - Deutsches Raspberry Pi Forum
June 9, 2017 - Die lassen eine Unterroutine entweder nach einer bestimmten Zeit (AFTER) oder periodisch immer wieder nach einer bestimmten Zeitspanne (EVERY) ablaufen, Das Hauptprogramm läuft dabei ungestört weiter und die CPU-Last während der Warteperiode ist 0. Das muesste es aber in python auch geben. ... Die Frage, die sich mir stellt, redest Du von Zeit-Intervallen im µs-Bereich, Sekunden-Bereich oder auch von Minuten bis Stunden? Bei Einsatz der üblichen Betriebssysteme ist der Raspberry Pi nicht echtzeitfähig, da die vielen parallel laufenden Prozesse keine Gewähr dafür geben, dass Deine Anwendung immer gleich viel Taktzeit bekommt.
🌐
Raspberry Pi
forums.raspberrypi.com › board index › programming › python
Timer interrupts python - Raspberry Pi Forums
That's why I had placed it interrupt so that it can execute in any case. ... void timer_isr(void) { timer_cnt++; if((timer_cnt % 100) == 0) { led_toggle(); } } void main(void) { while(1) { /* task_1 */ if((timer_cnt % 500) == 0) { task_1(); } /* task_2 */ if((timer_cnt % 1000) == 0) { task_2(); } /* reset timer */ if((timer_cnt > 20000)) { timer_cnt = 0 } } }
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › using the raspberry pi › beginners
Timer Interrupts - Raspberry Pi Forums
Thanks, Check out this link to SwitchDOC Labs DS1307 Python library... ... there are others too. Hi, thank you very much for responding. Looked through the link and it seems pretty helpful in describing general usage of the DS1307 and also how to test for time drift over a period of days or months etc. But what I would like to do is to test how precise the timer can be in a real-time application.
🌐
raspberry-pi-os
s-matyukevich.github.io › raspberry-pi-os › docs › lesson03 › rpi-os.html
3.1: Interrupts | raspberry-pi-os
Such asynchronous notifications are called “interrupts” because they interrupt normal execution flow and force the processor to execute an “interrupt handler”. There is one device that is particularly useful in operating system development: system timer.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › python
Ticker interrupt in Python? - Raspberry Pi Forums
Take picture. Turn off LED2. etc. etc. Alternatively, perhaps another solution to achieve the same aim...? Thank you. ... Python has timing capabilities, but cannot guarantee the precision of that timing. Here is an article that addresses the precision of using Python for timing events.
🌐
Flowcode
flowcode.co.uk › forums › viewtopic.php
Raspberry Pi interupt on timer - Flowcode Support Board
Bob ... From the ualarm docs: The ualarm() function causes the signal SIGALRM to be sent to the invoking process after (not less than) usecs microseconds. The delay may be lengthened slightly by any system activity or by the time spent processing the call or by the granularity of system timers.
🌐
Microcontrollers Lab
microcontrollerslab.com › home › raspberry pi pico › generate delay with raspberry pi pico timers using micropython
Generate Delay with Raspberry Pi Pico Timers using MicroPython
February 18, 2026 - For example, if we want to use virtual timer, the ID will be -1. Otherwise, you can leave the ID empty to use the single hardware timer of Raspberry Pi Pico. The timer is then initialized in the following way. The Timer() function has three arguments namely: Period: The first argument is that of the period of the interrupt signal in milliseconds.
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › bare metal, assembly language
Using the system timer interrupt at 1MHz - Raspberry Pi Forums
On linux there is no point you can't run 1uS interrupt rate you will struggle with that rate even in baremetal. Tightloop or fancy DMA is the only way you are going to do that rate on a Raspberry Pi.