import sched
import time
import functools


def repeat(func, interval, *args, **kwargs):
	"""
	Repeatedly calls a function. Blocks. Return True from the function to stop the repetition.

	:param func: The function to call.
	:param float interval: The interval between calls in seconds. The actual interval will NOT be exactly equal to this.
	:param args: The positional arguments to pass into func.
	:param kwargs: The keyword arguments to pass into func.
	"""
	scheduler = sched.scheduler(time.time, time.sleep)
	def repeat_decorator(func):
		@functools.wraps(func)
		def wrapper(*args, **kwargs):
			started = time.time()
			stopped = func(*args, **kwargs)
			if not stopped:
				scheduler.enter(interval - (time.time() - started), 1, wrapper, args, kwargs)
		return wrapper
	scheduler.enter(interval, 1, repeat_decorator(func), args, kwargs)
	scheduler.run()