3 ms·
Heh, a younger me would definitely be walking around with a black T-shirt with: from functions import * …on the front and @cached_property def troll_fa
by gorgoiler 5y ago
Heh, a younger me would definitely be walking around with a black T-shirt with:
from functions import *
…on the front and
@cached_property
def troll_face(self):
return ‘:)’
…on the back.
functools and itertools are amazing and I love them both. They are especially useful for teaching high school CS without having to stray from Python, which the kids at all levels know well and are comfortable with.
However.
Using @cached_property feels like a bad code smell and it’s a controversial design decision. The example given is more like an instance of a factory? Perhaps if the result of the render function was an Important Object™ (not just a mere string) then the function’s callers might not be so cavalier about discarding the generated instance.
I would like to see the calling code that calls `render` more than once in two different places (hence the need for caching with no option for cache invalidation?). When every stack frame is a descendant of main() then there’s no such thing as “two different places”!
def main():
page = HN().render()
while ‘too true’:
do_work(page)
tea_break(page)
Ironically it doesn’t feel very functional.
- wbradley 5y agoPlease do not promote the use of wildcard imports.
- matsemann 5y agoBecause of the GIL the cache is often pretty useless on a webserver anyway, as each and every request gets a different process not sharing that cache. So then one has to deploy Redis in addition and complicate the stack. Makes sense with a shared cache when there's multiple instances, but often it's overkill. Having to go "external" to fetch the data I might as well not cache anything.
- judofyr 5y ago> each and every request gets a different process not sharing that cache What web server is this? I’ve never heard about this behavior before.
- matsemann 5y agoI might have been a bit unclear. Not every request gets a new process, but there are a pool of processes. For instance gunicorn will maybe have ~20 processes ready to serve requests, with a main process sending work to the workers. Since it's different processes, there is very little communication or shared resources between them. It's almost as if you're running your main entry point multiple times in different terminals, just with gunicorn managing it for you. This is different than for instance java, which there is one process and then multiple threads. But because of the GIL in python, only one thread can ever run at the same time (in the same process), therefore one has to launch multiple instances/processes of the application.
- zbentley 5y agoThat's true, cache utilization declines as a function of your processing parallel (and in gunicorn/celery, your max-requests-per-process-before-suicide setting). Cached properties still can help a good deal though, especially when coupled with a) careful prefork initialization of cached data and b) even more careful use of multiprocessing's little-known shared memory interface which really does allow caches to be shared, without read locks, between multiple processes.
- sreeramb93 5y agoExactly. How do these caches behave when python is used as a webservice in a multi-process multi-thread setting. What if we have multiple containers running the same service and load balanced using an ingress controller? Local Caches are a headache in such a scenario. Best pattern I have read is use a cache as a sidecar in a pod. I feel that scales very well.
- TristanDaCunha 5y ago> Using @cached_property feels like a bad code smell and it’s a controversial design decision Why?
- gorgoiler 5y agoIt’s just a smell. It hints that instead of passing the instance, one should instead be passing the output of the function. It also doesn’t really matter. The only time things like this have actually been important was in giant Java codebases, where attempts were made to keep packages cleanly separated (and so therefore passing the simplest possible public types reduces the amount of public classes).