Implementing a decorator for thread synchronisation
summary
Shows how to implement Java-style synchronized in Python by building a decorator that locks per-instance, per-class or per-function, using wrapt to handle method binding and preserve introspection, and relying on setdefault atomic to create per-instance locks safely; supports using the decorator as a context manager too; reproduces the race where 100 threads expected 100 increments but got ~11 and explains why naive decorators and monkey-patching break in corner cases.
AI-written summary · about
chapters (34)
- 0:00Start
- 0:42Parallel execution using threads
- 2:27Manipulating a counter value
- 3:49Synchronisation using a lock
- 4:51Synchronisation in Java
- 5:37Can we achieve this in Python?
- 7:39Requires lock be a class attribute
- 8:53Store lock as an instance attribute
- 12:05Lock is automatically created
- 12:17Only works for instance methods
- 12:36Switch to the wrapt decorator package
- 13:41Implementing a universal decorator
- 15:09Rewrite decorator using wrapt
- 16:41Works on normal function
- 17:09What about static methods?
- 17:31And class methods?
- 18:10Extra trickery required
- 19:48Test class method again
- 20:00Verify no lock on class method Fails as expected
- 20:19Verify separate locks for instance and class
- 20:54Locking based on context used
- 21:40Locking on code blocks in Java
- 21:50Could we also do this?
- 22:01Type created by a decorator
- 22:28What the decorator did
- 22:53Rewrite to use function wrapper class
- 23:44Extend to be a context manager
- 24:26Synchronise across instance methods
- 25:02Synchronise access to class attributes
- 25:41Explicitly sharing a lock
- 26:09Detecting a resource lock Detect when lock object has been passed
- 27:00What about context manager?
- 28:40Reuse, don't reimplement
- 29:29Don't write decorators from scratch either
related talks
29mTackling Thread Safety in Python
02h 32mIntroduction to Decorators: Power UP Your Python Code
02h 21mIntroduction to Decorators: Power Up Your Python Code
02h 15mAll about decorators
03mLightning Talk: Python Decorators
50mUsing Asyncio in Python 3
01h 30mPython Design Patterns
31mHear no evil, see no evil, patch no evil: Or, how to monkey-patch safely.
