Menu

Why Your Python Cache Does the Same Work Twice (P.S: GIL Won’t Save You)
📰

Why Your Python Cache Does the Same Work Twice (P.S: GIL Won’t Save You)

#hackernoon#technology#cache#lock#thread
Reading 0:00
15s threshold

You added a cache so an expensive function would run once per key. Then you counted how often it really ran. For 50 keys, it ran about 400 times. The cache works. Every value it returns is correct. It just doesn’t do the one job you gave it. And the usual excuse, “Python has a GIL, so threads can’t collide,” doesn’t apply here.

In my last article, two objects shared one list by accident. Here, threads share a cache on purpose and still get surprised. The bug is not in the shared data. It is in the gap between two lines of code.

I ran every example in this article on CPython 3.12.3, the standard build with the GIL. Exact numbers will differ on your machine.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.