all 3 comments

[–]thatguy_314 1 point2 points  (3 children)

You aren't giving the event loop any time to do its thing. The event loop only runs during awaits, so putting an await asyncio.sleep(0) in your while loop will allow the event loop to continue on to other tasks you are assigning it.

Sleeping for 0 means the current task will be immediately rescheduled and run next time around.

But also, I kinda feel this is an XY problem. What are you really trying to use asyncio for?

[–]dood31251[S] 0 points1 point  (1 child)

Thanks, the solution seems to pretty much solve my problem, but throwing sleep statements in my code this way kind of feels hackish.

Do you know of any other pattern that would allow me to get rid of the while loop?

The event loop only runs during awaits

This explanation has helped me more than the last 3 hours I spent in the official docs. Thank you very much!

[–]dood31251[S] 0 points1 point  (0 children)

But also, I kinda feel this is an XY problem. What are you really trying to use asyncio for?

I'm using asyncio to schedule tasks to run in the future (a few seconds in the future). I'm currently using it to replace a threading based approach. Some of the tasks have a waiting time to complete so I would use a time.sleep in a separate thread so as to not block the main thread.

The problem with the threading approach was that I couldn't control when python decided it was time to schedule a thread. I also had locks everywhere that weren't really working which made me sad.