python-dependency-injector
python-dependency-injector copied to clipboard
Async resource is sometimes None
trafficstars
Hello, I am trying to provide a long-lived aiohttp.ClientSession for integrating with an external HTTP API.
I have an async generator for the resource defined as:
async def http_session_factory(*args, **kwargs):
session = ClientSession(*args, **kwargs)
yield session
if not session.closed:
await session.close()
which is then added to the DeclarativeContainer with providers.Resource(http_session_factory).
But, my application is intermittently throwing:
AttributeError: 'NoneType' object has no attribute 'request'"
when trying to use the provided ClientSession instance.
Am I initializing this incorrectly?
I am seeing similar behavior in flask but for a blocking/non-async resource. It seems to happen when there are several concurrent requests being handled by separate threads. Did you ever resolve the problem?