{"type":"rich","version":"1.0","provider_name":"Transistor","provider_url":"https://transistor.fm","author_name":"Data Science Tech Brief By HackerNoon","title":"Why Your Python Cache Does the Same Work Twice (P.S: GIL Won’t Save You)","html":"<iframe width=\"100%\" height=\"180\" frameborder=\"no\" scrolling=\"no\" seamless src=\"https://share.transistor.fm/e/ac74b270\"></iframe>","width":"100%","height":180,"duration":506,"description":"\n        This story was originally published on HackerNoon at: https://hackernoon.com/why-your-python-cache-does-the-same-work-twice-ps-gil-wont-save-you.\nYour Python cache can run the same work twice, even with the GIL. See the check-then-act race, the lru_cache gotcha, and how locks fix it.\nCheck more stories related to data-science at: https://hackernoon.com/c/data-science.\n            You can also check exclusive content about #python-programming, #python-threading, #race-conditions, #python-gil, #thread-safety, #python-concurrency, #hackernoon-top-story,  and more.\nThis story was written by: @sohelalam79. Learn more about this writer by checking @sohelalam79's about page,\n            and for more stories, please visit hackernoon.com.\nA cache that checks \"is this key missing?\" and then computes the value can run the same work many times when threads interleave, even with the GIL, because the GIL protects the interpreter, not your two-step logic. Hold one lock around the check and the work, ideally one lock per key.","thumbnail_url":"https://img.transistorcdn.com/8VxAgS1Ll3FJEERcAdhFdqqXJMnE7OfD2RUvrjauLt0/rs:fill:0:0:1/w:400/h:400/q:60/mb:500000/aHR0cHM6Ly9pbWct/dXBsb2FkLXByb2R1/Y3Rpb24udHJhbnNp/c3Rvci5mbS9zaG93/LzQxMjY4LzE2ODM1/ODI1ODUtYXJ0d29y/ay5qcGc.webp","thumbnail_width":300,"thumbnail_height":300}