0

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())
Jason
  • 404
  • 4
  • 14
  • `util.patchAsyncio` has to be called to do anything (`util.patchAsyncio()`). Not sure if that’s the problem though. – Ry- Jul 01 '22 at 20:24
  • It was intended to be called, but as you pointed out it was typo'd. It did not resolve the issue once corrected. Updated original post to reflect correct syntax for `util.patchAsyncio()`. – Jason Jul 01 '22 at 20:29
  • 2
    `pycron` runs the coroutine in a separate thread, which has its own event loop. This likely causes problems since you connect to `ib_sync` on the main thread. Try using a simple `while True` and an `asyncio.sleep` to repeat the code on an interval rather than using `pycron`. – bgfvdu3w Jul 01 '22 at 20:39
  • The intervals provided with a simple `.sleep()` are not robust enough for my use case. My production code requires that I run this every 1, 5, 15, 30 and 60 minutes, relative to the top of every hour. Pycron makes these intervals simple to produce so if possible working within that is ideal. – Jason Jul 01 '22 at 20:51
  • 1
    https://github.com/gawel/aiocron – Ry- Jul 01 '22 at 20:55
  • `aiocron` seems to be a good alternative for pycron. It resolved the issue. – Jason Jul 01 '22 at 21:13

0 Answers0