I have an asyncio project. It has several modules. Many of them need access to a few globals, like: 1. aiohttp ClientSession()
object, since according to aiohttp docs, I should avoid creating a new ClientSession for every request.
2. asyncio socket, i.e reader, writer
which I create using asyncio.open_connection()
. I want to maintain a persistent connection.
3. event loop, which I get using a asyncio.get_event_loop()
What is the best practice to share such variables?
I would like to create a globals.py
module, which will define these variables.
The problem is that I can't use the async with
syntax for the ClientSession
object in the globals module.
For the socket, I must define it somehow in an async def, so I can't expose it at the module level.
And, testing wise - should every module define a global variable like:
loop = asyncio.get_event_loop()
Or, is it better to pass the event loop to the module, for example in the class __init__
)?
Aucun commentaire:
Enregistrer un commentaire