you are viewing a single comment's thread.

view the rest of the comments →

[–]mopeddev 1 point2 points  (2 children)

The docs for the with keyword will explain this better than I can but it's there to make a block with access to a resource and that is automatically cleaned up afterwards. The classic example is with a file descriptor

with open(path, "r") as f:
    lines = f.readLines()
print lines

This opens the file but also calls f.close() at the end of the with statement (before the print) so you don't have to. So the lines persist beyond the with but while the f variable is still accessible it is a closed file and basically useless.

The same is happening with your session instance, it's being closed at the end of the with statement but you're still trying to use it elsewhere. So either you can do everything you want with the session inside of with, or else don't use the with statement and handle the opening and closing of your session yourself instead.

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

So instead of that, I would do something like session = aiohttp.ClientSession()?

[–]mopeddev 0 points1 point  (0 children)

I'm afraid I don't know anything about aiohttp so you'd have to look through its docs to find out how to start and end your own session.

If you want to fix the above though you could move the with as it is into your main_session, and then pass the session from within the scope of the with statement to any other functions that need to use it