Funktionierender Prototyp des Serious Games zur Vermittlung von Wissen zu Software-Engineering-Arbeitsmodellen.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

threads.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. # Copyright (c) Twisted Matrix Laboratories.
  2. # See LICENSE for details.
  3. """
  4. Extended thread dispatching support.
  5. For basic support see reactor threading API docs.
  6. """
  7. import queue as Queue
  8. from twisted.internet import defer
  9. from twisted.python import failure
  10. def deferToThreadPool(reactor, threadpool, f, *args, **kwargs):
  11. """
  12. Call the function C{f} using a thread from the given threadpool and return
  13. the result as a Deferred.
  14. This function is only used by client code which is maintaining its own
  15. threadpool. To run a function in the reactor's threadpool, use
  16. C{deferToThread}.
  17. @param reactor: The reactor in whose main thread the Deferred will be
  18. invoked.
  19. @param threadpool: An object which supports the C{callInThreadWithCallback}
  20. method of C{twisted.python.threadpool.ThreadPool}.
  21. @param f: The function to call.
  22. @param args: positional arguments to pass to f.
  23. @param kwargs: keyword arguments to pass to f.
  24. @return: A Deferred which fires a callback with the result of f, or an
  25. errback with a L{twisted.python.failure.Failure} if f throws an
  26. exception.
  27. """
  28. d = defer.Deferred()
  29. def onResult(success, result):
  30. if success:
  31. reactor.callFromThread(d.callback, result)
  32. else:
  33. reactor.callFromThread(d.errback, result)
  34. threadpool.callInThreadWithCallback(onResult, f, *args, **kwargs)
  35. return d
  36. def deferToThread(f, *args, **kwargs):
  37. """
  38. Run a function in a thread and return the result as a Deferred.
  39. @param f: The function to call.
  40. @param args: positional arguments to pass to f.
  41. @param kwargs: keyword arguments to pass to f.
  42. @return: A Deferred which fires a callback with the result of f,
  43. or an errback with a L{twisted.python.failure.Failure} if f throws
  44. an exception.
  45. """
  46. from twisted.internet import reactor
  47. return deferToThreadPool(reactor, reactor.getThreadPool(), f, *args, **kwargs)
  48. def _runMultiple(tupleList):
  49. """
  50. Run a list of functions.
  51. """
  52. for f, args, kwargs in tupleList:
  53. f(*args, **kwargs)
  54. def callMultipleInThread(tupleList):
  55. """
  56. Run a list of functions in the same thread.
  57. tupleList should be a list of (function, argsList, kwargsDict) tuples.
  58. """
  59. from twisted.internet import reactor
  60. reactor.callInThread(_runMultiple, tupleList)
  61. def blockingCallFromThread(reactor, f, *a, **kw):
  62. """
  63. Run a function in the reactor from a thread, and wait for the result
  64. synchronously. If the function returns a L{Deferred}, wait for its
  65. result and return that.
  66. @param reactor: The L{IReactorThreads} provider which will be used to
  67. schedule the function call.
  68. @param f: the callable to run in the reactor thread
  69. @type f: any callable.
  70. @param a: the arguments to pass to C{f}.
  71. @param kw: the keyword arguments to pass to C{f}.
  72. @return: the result of the L{Deferred} returned by C{f}, or the result
  73. of C{f} if it returns anything other than a L{Deferred}.
  74. @raise Exception: If C{f} raises a synchronous exception,
  75. C{blockingCallFromThread} will raise that exception. If C{f}
  76. returns a L{Deferred} which fires with a L{Failure},
  77. C{blockingCallFromThread} will raise that failure's exception (see
  78. L{Failure.raiseException}).
  79. """
  80. queue = Queue.Queue()
  81. def _callFromThread():
  82. result = defer.maybeDeferred(f, *a, **kw)
  83. result.addBoth(queue.put)
  84. reactor.callFromThread(_callFromThread)
  85. result = queue.get()
  86. if isinstance(result, failure.Failure):
  87. result.raiseException()
  88. return result
  89. __all__ = [
  90. "deferToThread",
  91. "deferToThreadPool",
  92. "callMultipleInThread",
  93. "blockingCallFromThread",
  94. ]