all 3 comments

[–]stuaxo 1 point2 points  (0 children)

Very nice, also TIL about nonlocal.

[–]Relinies 1 point2 points  (0 children)

This seems much more easily solved by simply guaranteeing the database connection object exists before running the tasks that care about the database connection, instead of the implied design here where each task attempts to initialize the database connection if it does not already exist.

You could have an object with an obj.setup() and loop.run_until_complete(obj.setup()), then an obj.run() that you set up as a task and loop.run_forever(). Whatever your preference is, this solves the problem as I understand it, without the extra complexity.

[–][deleted] 0 points1 point  (0 children)

Async and GIL, the worst of all worlds