File tree Expand file tree Collapse file tree 4 files changed +118
-0
lines changed
Expand file tree Collapse file tree 4 files changed +118
-0
lines changed Original file line number Diff line number Diff line change @@ -7,5 +7,6 @@ exclude .readthedocs.yaml
77
88recursive-include docs *
99prune docs/_build
10+ prune gists
1011
1112recursive-include tests *.py
Original file line number Diff line number Diff line change 1+ import threading
2+ from time import sleep
3+
4+ import cachetools
5+
6+ NUM_THREADS = 1000
7+ CACHE_TTL_SECONDS = 3600 # one hour
8+ INFO = True
9+
10+
11+ @cachetools .cached (
12+ cache = cachetools .TTLCache (maxsize = 1 , ttl = CACHE_TTL_SECONDS ),
13+ condition = threading .Condition (),
14+ info = INFO ,
15+ )
16+ def get_value ():
17+ print ("get_value" )
18+ sleep (1 )
19+ return 42
20+
21+
22+ class MyThread (threading .Thread ):
23+ def __init__ (self , id ):
24+ threading .Thread .__init__ (self )
25+ self .thread_name = str (id )
26+ self .thread_ID = id
27+
28+ def run (self ):
29+ get_value ()
30+
31+
32+ threads = []
33+ for i in range (0 , NUM_THREADS ):
34+ t = MyThread (i )
35+ threads .append (t )
36+ t .start ()
37+ for t in threads :
38+ t .join ()
39+ print (get_value .cache_info ())
Original file line number Diff line number Diff line change 1+ # https://github.com/tkem/cachetools/issues/294
2+ import threading
3+ from time import sleep
4+
5+ import cachetools
6+
7+ NUM_THREADS = 1000
8+ CACHE_TTL_SECONDS = 3600 # one hour
9+ # INFO = True
10+
11+
12+ class Cached :
13+
14+ def __init__ (self ):
15+ self .cache = cachetools .TTLCache (maxsize = 1 , ttl = CACHE_TTL_SECONDS )
16+ self .cond = threading .Condition ()
17+
18+ @cachetools .cachedmethod (lambda self : self .cache , condition = lambda self : self .cond )
19+ def get_value (self ):
20+ print ("get_value" )
21+ sleep (1 )
22+ return 42
23+
24+
25+ class MyThread (threading .Thread ):
26+ def __init__ (self , id , cached ):
27+ threading .Thread .__init__ (self )
28+ self .thread_name = str (id )
29+ self .thread_ID = id
30+ self .cached = cached
31+
32+ def run (self ):
33+ self .cached .get_value ()
34+
35+
36+ cached = Cached ()
37+ threads = []
38+ for i in range (0 , NUM_THREADS ):
39+ t = MyThread (i , cached )
40+ threads .append (t )
41+ t .start ()
42+ for t in threads :
43+ t .join ()
Original file line number Diff line number Diff line change 1+ # https://github.com/tkem/cachetools/issues/294
2+ import threading
3+ from time import sleep
4+
5+ import cachetools .func
6+
7+ NUM_THREADS = 1000
8+ CACHE_TTL_SECONDS = 3600 # one hour
9+
10+
11+ @cachetools .func .ttl_cache (maxsize = 1 , ttl = CACHE_TTL_SECONDS )
12+ def get_value ():
13+ print ("get_value" )
14+ sleep (1 )
15+ return 42
16+
17+
18+ class MyThread (threading .Thread ):
19+ def __init__ (self , id ):
20+ threading .Thread .__init__ (self )
21+ self .thread_name = str (id )
22+ self .thread_ID = id
23+
24+ def run (self ):
25+ get_value ()
26+
27+
28+ threads = []
29+ for i in range (0 , NUM_THREADS ):
30+ t = MyThread (i )
31+ threads .append (t )
32+ t .start ()
33+ for t in threads :
34+ t .join ()
35+ print (get_value .cache_info ())
You can’t perform that action at this time.
0 commit comments