News for hackers. Stuff that matters.
Re:Functools – The Power of Higher-Order Functions in Python by gorgoiler on Friday July 09, 2021 @12:01AM (#27780354)
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.
Re:Functools – The Power of Higher-Order Functions in Python by matsemann on Friday July 09, 2021 @02:47AM (#27781068)
Because 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.
Re:Functools – The Power of Higher-Order Functions in Python by judofyr on Friday July 09, 2021 @04:39AM (#27781633)
> 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.

Re:Functools – The Power of Higher-Order Functions in Python by matsemann on Friday July 09, 2021 @06:17AM (#27782207)
I 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.

Re:Functools – The Power of Higher-Order Functions in Python by zbentley on Friday July 09, 2021 @09:25AM (#27783427)
That'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.
Re:Functools – The Power of Higher-Order Functions in Python by sreeramb93 on Friday July 09, 2021 @05:43AM (#27782012)
Exactly. 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.

Re:Functools – The Power of Higher-Order Functions in Python by wbradley on Friday July 09, 2021 @01:36AM (#27780743)
Please do not promote the use of wildcard imports.
Re:Functools – The Power of Higher-Order Functions in Python by TristanDaCunha on Friday July 09, 2021 @04:56AM (#27781706)
> Using @cached_property feels like a bad code smell and it’s a controversial design decision

Why?

Re:Functools – The Power of Higher-Order Functions in Python by gorgoiler on Friday July 09, 2021 @08:25AM (#27782947)
It’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).