gawel / aiocron

Crontabs for asyncio
MIT License
338 stars 20 forks source link

aiocron 1.7 broken when providing paramaters #24

Closed benoitscherrer closed 2 years ago

benoitscherrer commented 2 years ago

It seems aiocron 1.7 broke something when calling a coroutine with parameters Here using python 3.8.10 on Ubuntu

For example that works:

import asyncio
import datetime
from aiocron import crontab
async def working():
    print("OK working")

async def notworking(param):
    print("Working", param)

async def mymain():
    time=datetime.datetime.now().time()
    print(f"Will fire at {time.hour}:{time.minute+1}")

    crontab(f"{time.minute+1} {time.hour} * * *", start=True, func=working)
    crontab(f"{time.minute+1} {time.hour} * * *", start=True, func=notworking, args=(1,))

    while 1==1:
        print(f"Now is {datetime.datetime.now().time()}")
        await asyncio.sleep(1)

def main():
     loop = asyncio.get_event_loop()
     loop.run_until_complete(mymain())

if __name__ == '__main__':
    main()

I get something like:

Now is 10:43:58.432655
Now is 10:43:59.434129
Working 1
OK working
Now is 10:44:00.435700
Now is 10:44:01.437527

However, in a class like that:

import asyncio
import datetime
from aiocron import crontab

class Test:
    def __init__(self):
        time=datetime.datetime.now().time()
        print(f"Will fire at {time.hour}:{time.minute+1}")
        crontab(f"{time.minute+1} {time.hour} * * *", start=True, func=self.working)
        crontab(f"{time.minute+1} {time.hour} * * *", start=True, func=self.notworking, args=(1,))

    async def run(self):
        while 1==1:
            print(f"Now is {datetime.datetime.now().time()}")
            await asyncio.sleep(1)

    async def working(self):
        print("OK working")

    async def notworking(self, param):
        print("Not working", param)

def main():
    test = Test()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test.run())

if __name__ == '__main__':
    main()

The call without parameters works, but not the one with parameters. There is a RuntimeWarning: coroutine 'Test.notworking' was never awaited error (same if we create the crontab in run()) and the coroutine is never called (quite bad for our software! it was not doing anymore what it was supposed to do at regular intervals)

Now is 10:44:58.583610
Now is 10:44:59.585436
OK working
/usr/lib/python3.8/asyncio/base_events.py:1844: RuntimeWarning: coroutine 'Test.notworking' was never awaited
  handle = self._ready.popleft()
RuntimeWarning: Enable tracemalloc to get the object allocation traceback
Now is 10:45:00.588671
Now is 10:45:01.590473

Thanks so much B

gawel commented 2 years ago

Just tried your example with a class and python3.9. Works fine:

Now is 17:17:59.479047
OK working
Not working 1
gawel commented 2 years ago

Same with python 3.8.12

Can you try after pip install -e "git+ssh://git@github.com:gawel/aiocron.git@master#egg=aiocron" ?

gawel commented 2 years ago

Ok you're experimenting an already fixed bug: https://github.com/gawel/aiocron/commit/5b9ada7b8811c18c704f764b217d8e96c8e2d38b

I've just released aiocron 1.8. Should fix your problem.

benoitscherrer commented 2 years ago

Thanks! Yes I confirm that with 1.8 it works.

Thanks B