Why does an awaited python coroutine not run (or finish?)?

152 Views Asked by At

In this minimum reproducible example, every minute at :00 I'm expecting to see the output of future stuff, but I don't.

Instead, all I get every :00 is:

# python3 test2.py
firing..
stuff
<class 'asyncio.futures.Future'>
firing..
stuff
<class 'asyncio.futures.Future'>
firing..
stuff
<class 'asyncio.futures.Future'>
...

There is no error returned. Naively, since the coroutine print_stuff() is called, at least it's somewhat correct code. Equally as a niave attempt to prove that at least ib.reqAllOpenOrdersAsync() is awaitable, I've typed it, and it seems to indicate that it's correct but I'm very new to asyncio so maybe that's meaningless. I've also tried stuff = await asyncio.gather(ib.reqAllOpenOrdersAsync()) with the same result.

# test2.py
import asyncio
from ib_insync import IB, util
import pycron
import datetime

ib = IB()

util.patchAsyncio()


@pycron.cron("* * * * *")
async def test(timestamp: datetime):
    print("firing..")
    await print_stuff()
    print(type(ib.reqAllOpenOrdersAsync()))
    stuff = await ib.reqAllOpenOrdersAsync()
    await print_stuff()
    print(f"did i finish {stuff}")


async def print_stuff():
    print("stuff")


async def main():
    await ib.connectAsync()
    pycron.start()


asyncio.run(main())
0

There are 0 best solutions below