threadutil.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. """
  2. Threading abstraction which allows for :mod:`threading2` use with a
  3. transparent fallback to :mod:`threading` when it is not available.
  4. Pyro doesn't use :mod:`threading` directly: it imports all
  5. thread related items via this module instead. Code using Pyro can do
  6. the same (but it is not required).
  7. Pyro - Python Remote Objects. Copyright by Irmen de Jong (irmen@razorvine.net).
  8. """
  9. from Pyro4 import config
  10. if config.THREADING2:
  11. try:
  12. from threading2 import *
  13. except ImportError:
  14. from threading import *
  15. else:
  16. from threading import *
  17. class AtomicCounter(object):
  18. def __init__(self, value=0):
  19. self.__initial = value
  20. self.__value = value
  21. self.__lock = Lock()
  22. def reset(self):
  23. self.__value = self.__initial
  24. def incr(self, amount=1):
  25. with self.__lock:
  26. self.__value += amount
  27. return self.__value
  28. def decr(self, amount=1):
  29. with self.__lock:
  30. self.__value -= amount
  31. return self.__value
  32. @property
  33. def value(self):
  34. return self.__value