Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Implementing a decorator for thread synchronisation.

338 views

Published on

Using multithreading in Python applications? Interested in complex use cases for decorators and context managers? This talk will describe how the Java programming language "synchronized" keyword, for handling synchronisation in multithread applications, can be implemented in Python.

Published in: Software
  • Be the first to comment

Implementing a decorator for thread synchronisation.

  1. 1. Implementing a decorator for thread synchronisation Graham Dumpleton Graham.Dumpleton@gmail.com @GrahamDumpleton
  2. 2. Parallel execution using threads Create threads Wait to finish Task to run
  3. 3. Manipulating a counter value Aiya!!!
  4. 4. Synchronisation using a lock Hooray Acquire lock
  5. 5. Synchronisation in Java
  6. 6. Can we achieve this in Python? Decorator
  7. 7. Lock object required Need argument
  8. 8. Requires lock be a class attribute Create lock Locks across all classes :-(
  9. 9. Store lock as an instance attribute Create lock if necessary Argument for self
 in wrapper
  10. 10. Lock is automatically created No argument
  11. 11. Only works for instance methods
  12. 12. Switch to the wrapt decorator package
  13. 13. Basic decorator with wrapt
  14. 14. Implementing a universal decorator
  15. 15. Rewrite decorator using wrapt Choose context where lock created
  16. 16. Works on normal function Locked on function
  17. 17. Works on instance methods Locked on instance
  18. 18. Verify no lock on class Fails as expected
  19. 19. What about static methods? Locked on static method
  20. 20. And class methods? Not a dict
  21. 21. Extra trickery required Use our own lock Can't use setdefault()
  22. 22. Test class method again Locked on class
  23. 23. Verify no lock on class method Fails as expected
  24. 24. Calling both instance and class methods
  25. 25. Verify separate locks for instance and class Different Did not accidentally fall through to using lock on the class
  26. 26. Locking based on context used
  27. 27. Locking on code blocks in Java
  28. 28. Could we also do this? Use as a context manager
  29. 29. Type created by a decorator Function is replaced with instance of
 FunctionWrapper
  30. 30. What the decorator did Decorator is a fancy way of doing this
  31. 31. Rewrite to use function wrapper class Create lock if necessary Acquire lock Wrap function
  32. 32. Extend to be a context manager Release lock Acquire lock
  33. 33. Synchronise across instance methods Lock only for code where
 is required
  34. 34. Synchronise access to class attributes Supply class instead of the instance Can be used in synchronised instance method
  35. 35. Explicitly sharing a lock
  36. 36. Detecting a resource lock Detect when lock object has been passed Can use a simple decorator
  37. 37. What about context manager? Could just use lock itself but makes it look consistent
  38. 38. Wrap with callable object proxy Intercept when used as context manager
  39. 39. Not all necessary but looks nice
  40. 40. Reuse, don't reimplement Use it from wrapt
  41. 41. Don't write decorators from scratch either https://pypi.org/project/wrapt/ http://wrapt.readthedocs.org/ https://github.com/GrahamDumpleton/wrapt

×