You can use signals and a context manager to acheive this. The idea is to register a function that will raise an exeption when the alarm signal is received and schedule the alarm signal for the timeout.
import signal from contextlib import contextmanager @contextmanager def timeout(time): # Register a function …read more