import asyncio


class Timer:
    def __init__(self, timeout, callback):
        self._timeout = timeout
        self._callback = callback
        self._task = asyncio.ensure_future(self.__job())
        self._cancelled = False

    async def __job(self):
        while not self._cancelled:
            await asyncio.sleep(self._timeout)
            await self._callback()

    def cancel(self):
        self._cancelled = True
        self._task.cancel()
