_convenience.py 969 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. # -*- test-case-name: twisted._threads.test.test_convenience -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Common functionality used within the implementation of various workers.
  6. """
  7. from __future__ import absolute_import, division, print_function
  8. from ._ithreads import AlreadyQuit
  9. class Quit(object):
  10. """
  11. A flag representing whether a worker has been quit.
  12. @ivar isSet: Whether this flag is set.
  13. @type isSet: L{bool}
  14. """
  15. def __init__(self):
  16. """
  17. Create a L{Quit} un-set.
  18. """
  19. self.isSet = False
  20. def set(self):
  21. """
  22. Set the flag if it has not been set.
  23. @raise AlreadyQuit: If it has been set.
  24. """
  25. self.check()
  26. self.isSet = True
  27. def check(self):
  28. """
  29. Check if the flag has been set.
  30. @raise AlreadyQuit: If it has been set.
  31. """
  32. if self.isSet:
  33. raise AlreadyQuit()