_ithreads.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # -*- test-case-name: twisted._threads.test -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Interfaces related to threads.
  6. """
  7. from typing import Callable
  8. from zope.interface import Interface
  9. class AlreadyQuit(Exception):
  10. """
  11. This worker worker is dead and cannot execute more instructions.
  12. """
  13. class IWorker(Interface):
  14. """
  15. A worker that can perform some work concurrently.
  16. All methods on this interface must be thread-safe.
  17. """
  18. def do(task: Callable[[], None]) -> None:
  19. """
  20. Perform the given task.
  21. As an interface, this method makes no specific claims about concurrent
  22. execution. An L{IWorker}'s C{do} implementation may defer execution
  23. for later on the same thread, immediately on a different thread, or
  24. some combination of the two. It is valid for a C{do} method to
  25. schedule C{task} in such a way that it may never be executed.
  26. It is important for some implementations to provide specific properties
  27. with respect to where C{task} is executed, of course, and client code
  28. may rely on a more specific implementation of C{do} than L{IWorker}.
  29. @param task: a task to call in a thread or other concurrent context.
  30. @type task: 0-argument callable
  31. @raise AlreadyQuit: if C{quit} has been called.
  32. """
  33. def quit() -> None:
  34. """
  35. Free any resources associated with this L{IWorker} and cause it to
  36. reject all future work.
  37. @raise AlreadyQuit: if this method has already been called.
  38. """
  39. class IExclusiveWorker(IWorker):
  40. """
  41. Like L{IWorker}, but with the additional guarantee that the callables
  42. passed to C{do} will not be called exclusively with each other.
  43. """