1414from collections import OrderedDict
1515from concurrent .futures import ThreadPoolExecutor
1616from functools import wraps
17- from typing import Optional , Union
17+ from typing import Any , Optional , Union
1818from warnings import warn
1919
2020from .config import (
@@ -55,13 +55,11 @@ def _function_thread(core, key, func, args, kwds):
5555 print (f"Function call failed with the following exception:\n { exc } " )
5656
5757
58- def _calc_entry (core , key , func , args , kwds ):
58+ def _calc_entry (core , key , func , args , kwds ) -> Optional [Any ]:
59+ core .mark_entry_being_calculated (key )
5960 try :
60- core .mark_entry_being_calculated (key )
61- # _get_executor().submit(core.mark_entry_being_calculated, key)
6261 func_res = func (* args , ** kwds )
6362 core .set_entry (key , func_res )
64- # _get_executor().submit(core.set_entry, key, func_res)
6563 return func_res
6664 finally :
6765 core .mark_entry_not_calculated (key )
@@ -242,9 +240,8 @@ def func_wrapper(*args, **kwds):
242240 func , _is_method = core .func_is_method , args = args , kwds = kwds
243241 )
244242
245- _print = lambda x : None # noqa: E731
246- if verbose :
247- _print = print
243+ _print = print if verbose else lambda x : None
244+
248245 if ignore_cache or not _global_params .caching_enabled :
249246 return (
250247 func (args [0 ], ** kwargs )
@@ -254,7 +251,9 @@ def func_wrapper(*args, **kwds):
254251 key , entry = core .get_entry ((), kwargs )
255252 if overwrite_cache :
256253 return _calc_entry (core , key , func , args , kwds )
257- if entry is None :
254+ if entry is None or (
255+ not entry ._completed and not entry ._processing
256+ ):
258257 _print ("No entry found. No current calc. Calling like a boss." )
259258 return _calc_entry (core , key , func , args , kwds )
260259 _print ("Entry found." )
@@ -265,7 +264,7 @@ def func_wrapper(*args, **kwds):
265264 _print ("And it is fresh!" )
266265 return entry .value
267266 _print ("But it is stale... :(" )
268- if entry .being_calculated :
267+ if entry ._processing :
269268 if _next_time :
270269 _print ("Returning stale." )
271270 return entry .value # return stale val
@@ -276,8 +275,8 @@ def func_wrapper(*args, **kwds):
276275 return _calc_entry (core , key , func , args , kwds )
277276 if _next_time :
278277 _print ("Async calc and return stale" )
278+ core .mark_entry_being_calculated (key )
279279 try :
280- core .mark_entry_being_calculated (key )
281280 _get_executor ().submit (
282281 _function_thread , core , key , func , args , kwds
283282 )
@@ -286,7 +285,7 @@ def func_wrapper(*args, **kwds):
286285 return entry .value
287286 _print ("Calling decorated function and waiting" )
288287 return _calc_entry (core , key , func , args , kwds )
289- if entry .being_calculated :
288+ if entry ._processing :
290289 _print ("No value but being calculated. Waiting." )
291290 try :
292291 return core .wait_on_entry_calc (key )
0 commit comments