gawel / aiocron

Crontabs for asyncio
MIT License
338 stars 20 forks source link

Handle awaitables returned from non-coroutine functions #29

Closed SF-300 closed 2 years ago

SF-300 commented 2 years ago

Hi!

FIrst of all thanks for this very handy package to all its contributors!

Unfortunately I've stumbled upon a problem. Cron object can handle plain old functions and coroutine functions perfectly fine. However there is no support for plain old functions returning Awaitable[...] instances i.e.:

import asyncio
from typing import Awaitable

import aiocron

async def test():
    print("It works!")

@aiocron.crontab("*/1 * * * *")
def callback() -> Awaitable[None]:
    return test()

asyncio.get_event_loop().run_forever()

There is also such message in the stderr:

C:\Users\-----\AppData\Local\Programs\Python\Python38\lib\asyncio\base_events.py:1860: RuntimeWarning: coroutine 'test' was never awaited
  handle = None  # Needed to break cycles when an exception occurs.
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

Actually it's quite easy to nest such functions inside proper coroutine. But I've spent a couple of hours trying to figure out why my callbacks are not invoked. It would be great to spare others from this kind of frustration =)