import asyncio


class Ticker:
    def __init__(self, timeout, callback, id=None):
        self._timeout = timeout
        self._callback = callback
        self._task = asyncio.create_task(self.__job())
        self._cancelled = False
        self._id = id

    async def __job(self):
        while True:
            await self._callback(self._id)
            await asyncio.sleep(self._timeout)

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