Periodic functions

The MicroAgent method can be run periodically after a certain period of time or on a schedule (cron).

Periodic calls are implemented with asyncio.call_later chains. Before each method call, the next call is initiated. Each call is independent, and previous calls do not affect subsequent calls. Exceptions are written to the logger in the associated Microagent.

class Agent(MicroAgent):

    @periodic(period=3, timeout=10, start_after=2)  # in seconds
    async def periodic_handler(self):
        pass  # code here

    @cron('*/10 * * * *', timeout=10)  # in seconds
    async def cron_handler(self):
        pass  # code here
microagent.cron(spec: str, timeout: int | float = 1) Callable[[Callable[[Any], Awaitable[None]]], Callable[[Any], Awaitable[None]]][source]

Run decorated function by schedule (cron)

Parameters:
  • spec – Specified running scheduling in cron format

  • timeout – Function timeout in seconds

@periodic('0 */4 * * *')
async def handler_1(self):
    log.info('Called handler 1')

@periodic('*/15 * * * *', timeout=10)
async def handler_2(self):
    log.info('Called handler 2')
microagent.periodic(period: int | float, timeout: int | float = 1, start_after: int | float = 0) Callable[[Callable[[Any], Awaitable[None]]], Callable[[Any], Awaitable[None]]][source]

Run decorated handler periodically.

Parameters:
  • period – Period of running functions in seconds

  • timeout – Function timeout in seconds

  • start_after – Delay for running loop in seconds

@periodic(period=5)
async def handler_1(self):
    log.info('Called handler 1')

@periodic(5, timeout=4)
async def handler_2(self):
    log.info('Called handler 2')

@periodic(period=5, start_after=10)
async def handler_3(self):
    log.info('Called handler 3')
class microagent.timer.PeriodicTask(agent: 'MicroAgent', handler: collections.abc.Callable[[Any], collections.abc.Awaitable[None]], period: float, timeout: float, start_after: float)[source]
class microagent.timer.CRONTask(agent: 'MicroAgent', handler: collections.abc.Callable[[Any], collections.abc.Awaitable[None]], cron: microagent.timer.CRON, timeout: float)[source]
property start_after: float

start_after property of CRONTask object is a next value of generator behind facade. Be carefully with manual manipulation with it.

property period: float

period property of CRONTask object is a next value of generator behind facade. Be carefully with manual manipulation with it.