Issue
I just read this blog post about a recipe to lazily initialize an object property. I am a recovering java programmer and if this code was translated into java, it would be considered a race condition (double check locking). Why does it work in python ? I know there is a threading module in python. Are locks added surreptitiously by the interpreter to make this thread-safe?
How does canonical thread-safe initialisation look in Python?
Solution
- No, no locks are added automatically.
- That's why this code is not thread-safe.
- If it seems to work in a multi-threaded program without problems, it's probably due to the Global Interpreter Lock, which makes the hazard less likely to occur.
Answered By - Niklas B.
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.