Don't catch key errors from the yielded to scope

context_caching
Tyler Goodlet 2021-12-14 10:55:27 -05:00
parent b210278e2f
commit 3826bc9972
1 changed files with 9 additions and 7 deletions

View File

@ -161,19 +161,15 @@ async def maybe_open_context(
a _Cache hit. a _Cache hit.
''' '''
# lock resource acquisition around task racing / ``trio``'s
# scheduler protocol
await _Cache.lock.acquire() await _Cache.lock.acquire()
ctx_key = id(mngr) ctx_key = id(mngr)
value = None value = None
try: try:
# lock feed acquisition around task racing / ``trio``'s
# scheduler protocol
value = _Cache.values[key] value = _Cache.values[key]
log.info(f'Reusing _Cached resource for {key}')
_Cache.users += 1
_Cache.lock.release()
yield True, value
except KeyError: except KeyError:
log.info(f'Allocating new resource for {key}') log.info(f'Allocating new resource for {key}')
@ -196,6 +192,12 @@ async def maybe_open_context(
yield False, value yield False, value
else:
log.info(f'Reusing _Cached resource for {key}')
_Cache.users += 1
_Cache.lock.release()
yield True, value
finally: finally:
_Cache.users -= 1 _Cache.users -= 1