Aiohttp ClientSession outside coroutine Aiohttp ClientSession outside coroutine multithreading multithreading

Aiohttp ClientSession outside coroutine


Why is it a problem starting a ClientSession outside of a coroutine?

That's how aiohttp is built, in theory it should be possible to initialize some kind of client session outside a loop ie. outside a coroutine but it's not how aiohttp is built. AFAIU in the issue that introduced this warning, it's because a) it's difficult to test b) it's prone to error

Is the queue approach better/safer? If so, why?

I don't understand what you try to achieve so I am not sure how to answer. Maybe the problem you have is that you try to initialize a ClientSession inside the constructor aka. __init__ of another class. In this case, you should work around that issue by creating a helper method that is a coroutine that will finish the initialization of the class. This is known pattern when working with async code.

Is there any problem in my approach for starting a loop inside a background thread?

It's perfectly ok.