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.

task.py 33KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973
  1. # -*- test-case-name: twisted.test.test_task,twisted.test.test_cooperator -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Scheduling utility methods and classes.
  6. """
  7. import sys
  8. import time
  9. import warnings
  10. from typing import (
  11. Callable,
  12. Coroutine,
  13. Iterable,
  14. Iterator,
  15. List,
  16. NoReturn,
  17. Optional,
  18. Sequence,
  19. TypeVar,
  20. Union,
  21. cast,
  22. )
  23. from zope.interface import implementer
  24. from incremental import Version
  25. from twisted.internet.base import DelayedCall
  26. from twisted.internet.defer import Deferred, ensureDeferred, maybeDeferred
  27. from twisted.internet.error import ReactorNotRunning
  28. from twisted.internet.interfaces import IDelayedCall, IReactorCore, IReactorTime
  29. from twisted.python import log, reflect
  30. from twisted.python.deprecate import _getDeprecationWarningString
  31. from twisted.python.failure import Failure
  32. _T = TypeVar("_T")
  33. class LoopingCall:
  34. """Call a function repeatedly.
  35. If C{f} returns a deferred, rescheduling will not take place until the
  36. deferred has fired. The result value is ignored.
  37. @ivar f: The function to call.
  38. @ivar a: A tuple of arguments to pass the function.
  39. @ivar kw: A dictionary of keyword arguments to pass to the function.
  40. @ivar clock: A provider of
  41. L{twisted.internet.interfaces.IReactorTime}. The default is
  42. L{twisted.internet.reactor}. Feel free to set this to
  43. something else, but it probably ought to be set *before*
  44. calling L{start}.
  45. @ivar running: A flag which is C{True} while C{f} is scheduled to be called
  46. (or is currently being called). It is set to C{True} when L{start} is
  47. called and set to C{False} when L{stop} is called or if C{f} raises an
  48. exception. In either case, it will be C{False} by the time the
  49. C{Deferred} returned by L{start} fires its callback or errback.
  50. @ivar _realLastTime: When counting skips, the time at which the skip
  51. counter was last invoked.
  52. @ivar _runAtStart: A flag indicating whether the 'now' argument was passed
  53. to L{LoopingCall.start}.
  54. """
  55. call: Optional[IDelayedCall] = None
  56. running = False
  57. _deferred: Optional[Deferred["LoopingCall"]] = None
  58. interval: Optional[float] = None
  59. _runAtStart = False
  60. starttime: Optional[float] = None
  61. _realLastTime: Optional[float] = None
  62. def __init__(self, f: Callable[..., object], *a: object, **kw: object) -> None:
  63. self.f = f
  64. self.a = a
  65. self.kw = kw
  66. from twisted.internet import reactor
  67. self.clock = cast(IReactorTime, reactor)
  68. @property
  69. def deferred(self) -> Optional[Deferred["LoopingCall"]]:
  70. """
  71. DEPRECATED. L{Deferred} fired when loop stops or fails.
  72. Use the L{Deferred} returned by L{LoopingCall.start}.
  73. """
  74. warningString = _getDeprecationWarningString(
  75. "twisted.internet.task.LoopingCall.deferred",
  76. Version("Twisted", 16, 0, 0),
  77. replacement="the deferred returned by start()",
  78. )
  79. warnings.warn(warningString, DeprecationWarning, stacklevel=2)
  80. return self._deferred
  81. @classmethod
  82. def withCount(cls, countCallable: Callable[[int], object]) -> "LoopingCall":
  83. """
  84. An alternate constructor for L{LoopingCall} that makes available the
  85. number of calls which should have occurred since it was last invoked.
  86. Note that this number is an C{int} value; It represents the discrete
  87. number of calls that should have been made. For example, if you are
  88. using a looping call to display an animation with discrete frames, this
  89. number would be the number of frames to advance.
  90. The count is normally 1, but can be higher. For example, if the reactor
  91. is blocked and takes too long to invoke the L{LoopingCall}, a Deferred
  92. returned from a previous call is not fired before an interval has
  93. elapsed, or if the callable itself blocks for longer than an interval,
  94. preventing I{itself} from being called.
  95. When running with an interval of 0, count will be always 1.
  96. @param countCallable: A callable that will be invoked each time the
  97. resulting LoopingCall is run, with an integer specifying the number
  98. of calls that should have been invoked.
  99. @return: An instance of L{LoopingCall} with call counting enabled,
  100. which provides the count as the first positional argument.
  101. @since: 9.0
  102. """
  103. def counter() -> object:
  104. now = self.clock.seconds()
  105. if self.interval == 0:
  106. self._realLastTime = now
  107. return countCallable(1)
  108. lastTime = self._realLastTime
  109. if lastTime is None:
  110. assert (
  111. self.starttime is not None
  112. ), "LoopingCall called before it was started"
  113. lastTime = self.starttime
  114. if self._runAtStart:
  115. assert (
  116. self.interval is not None
  117. ), "Looping call called with None interval"
  118. lastTime -= self.interval
  119. lastInterval = self._intervalOf(lastTime)
  120. thisInterval = self._intervalOf(now)
  121. count = thisInterval - lastInterval
  122. if count > 0:
  123. self._realLastTime = now
  124. return countCallable(count)
  125. return None
  126. self = cls(counter)
  127. return self
  128. def _intervalOf(self, t: float) -> int:
  129. """
  130. Determine the number of intervals passed as of the given point in
  131. time.
  132. @param t: The specified time (from the start of the L{LoopingCall}) to
  133. be measured in intervals
  134. @return: The C{int} number of intervals which have passed as of the
  135. given point in time.
  136. """
  137. assert self.starttime is not None
  138. assert self.interval is not None
  139. elapsedTime = t - self.starttime
  140. intervalNum = int(elapsedTime / self.interval)
  141. return intervalNum
  142. def start(self, interval: float, now: bool = True) -> Deferred["LoopingCall"]:
  143. """
  144. Start running function every interval seconds.
  145. @param interval: The number of seconds between calls. May be
  146. less than one. Precision will depend on the underlying
  147. platform, the available hardware, and the load on the system.
  148. @param now: If True, run this call right now. Otherwise, wait
  149. until the interval has elapsed before beginning.
  150. @return: A Deferred whose callback will be invoked with
  151. C{self} when C{self.stop} is called, or whose errback will be
  152. invoked when the function raises an exception or returned a
  153. deferred that has its errback invoked.
  154. """
  155. assert not self.running, "Tried to start an already running " "LoopingCall."
  156. if interval < 0:
  157. raise ValueError("interval must be >= 0")
  158. self.running = True
  159. # Loop might fail to start and then self._deferred will be cleared.
  160. # This why the local C{deferred} variable is used.
  161. deferred = self._deferred = Deferred()
  162. self.starttime = self.clock.seconds()
  163. self.interval = interval
  164. self._runAtStart = now
  165. if now:
  166. self()
  167. else:
  168. self._scheduleFrom(self.starttime)
  169. return deferred
  170. def stop(self) -> None:
  171. """Stop running function."""
  172. assert self.running, "Tried to stop a LoopingCall that was " "not running."
  173. self.running = False
  174. if self.call is not None:
  175. self.call.cancel()
  176. self.call = None
  177. d, self._deferred = self._deferred, None
  178. assert d is not None
  179. d.callback(self)
  180. def reset(self) -> None:
  181. """
  182. Skip the next iteration and reset the timer.
  183. @since: 11.1
  184. """
  185. assert self.running, "Tried to reset a LoopingCall that was " "not running."
  186. if self.call is not None:
  187. self.call.cancel()
  188. self.call = None
  189. self.starttime = self.clock.seconds()
  190. self._scheduleFrom(self.starttime)
  191. def __call__(self) -> None:
  192. def cb(result: object) -> None:
  193. if self.running:
  194. self._scheduleFrom(self.clock.seconds())
  195. else:
  196. d, self._deferred = self._deferred, None
  197. assert d is not None
  198. d.callback(self)
  199. def eb(failure: Failure) -> None:
  200. self.running = False
  201. d, self._deferred = self._deferred, None
  202. assert d is not None
  203. d.errback(failure)
  204. self.call = None
  205. d = maybeDeferred(self.f, *self.a, **self.kw)
  206. d.addCallback(cb)
  207. d.addErrback(eb)
  208. def _scheduleFrom(self, when: float) -> None:
  209. """
  210. Schedule the next iteration of this looping call.
  211. @param when: The present time from whence the call is scheduled.
  212. """
  213. def howLong() -> float:
  214. # How long should it take until the next invocation of our
  215. # callable? Split out into a function because there are multiple
  216. # places we want to 'return' out of this.
  217. if self.interval == 0:
  218. # If the interval is 0, just go as fast as possible, always
  219. # return zero, call ourselves ASAP.
  220. return 0
  221. # Compute the time until the next interval; how long has this call
  222. # been running for?
  223. assert self.starttime is not None
  224. runningFor = when - self.starttime
  225. # And based on that start time, when does the current interval end?
  226. assert self.interval is not None
  227. untilNextInterval = self.interval - (runningFor % self.interval)
  228. # Now that we know how long it would be, we have to tell if the
  229. # number is effectively zero. However, we can't just test against
  230. # zero. If a number with a small exponent is added to a number
  231. # with a large exponent, it may be so small that the digits just
  232. # fall off the end, which means that adding the increment makes no
  233. # difference; it's time to tick over into the next interval.
  234. if when == when + untilNextInterval:
  235. # If it's effectively zero, then we need to add another
  236. # interval.
  237. return self.interval
  238. # Finally, if everything else is normal, we just return the
  239. # computed delay.
  240. return untilNextInterval
  241. self.call = self.clock.callLater(howLong(), self)
  242. def __repr__(self) -> str:
  243. # This code should be replaced by a utility function in reflect;
  244. # see ticket #6066:
  245. func = getattr(self.f, "__qualname__", None)
  246. if func is None:
  247. func = getattr(self.f, "__name__", None)
  248. if func is not None:
  249. imClass = getattr(self.f, "im_class", None)
  250. if imClass is not None:
  251. func = f"{imClass}.{func}"
  252. if func is None:
  253. func = reflect.safe_repr(self.f)
  254. return "LoopingCall<{!r}>({}, *{}, **{})".format(
  255. self.interval,
  256. func,
  257. reflect.safe_repr(self.a),
  258. reflect.safe_repr(self.kw),
  259. )
  260. class SchedulerError(Exception):
  261. """
  262. The operation could not be completed because the scheduler or one of its
  263. tasks was in an invalid state. This exception should not be raised
  264. directly, but is a superclass of various scheduler-state-related
  265. exceptions.
  266. """
  267. class SchedulerStopped(SchedulerError):
  268. """
  269. The operation could not complete because the scheduler was stopped in
  270. progress or was already stopped.
  271. """
  272. class TaskFinished(SchedulerError):
  273. """
  274. The operation could not complete because the task was already completed,
  275. stopped, encountered an error or otherwise permanently stopped running.
  276. """
  277. class TaskDone(TaskFinished):
  278. """
  279. The operation could not complete because the task was already completed.
  280. """
  281. class TaskStopped(TaskFinished):
  282. """
  283. The operation could not complete because the task was stopped.
  284. """
  285. class TaskFailed(TaskFinished):
  286. """
  287. The operation could not complete because the task died with an unhandled
  288. error.
  289. """
  290. class NotPaused(SchedulerError):
  291. """
  292. This exception is raised when a task is resumed which was not previously
  293. paused.
  294. """
  295. class _Timer:
  296. MAX_SLICE = 0.01
  297. def __init__(self) -> None:
  298. self.end = time.time() + self.MAX_SLICE
  299. def __call__(self) -> bool:
  300. return time.time() >= self.end
  301. _EPSILON = 0.00000001
  302. def _defaultScheduler(callable: Callable[[], None]) -> IDelayedCall:
  303. from twisted.internet import reactor
  304. return cast(IReactorTime, reactor).callLater(_EPSILON, callable)
  305. _TaskResultT = TypeVar("_TaskResultT")
  306. class CooperativeTask:
  307. """
  308. A L{CooperativeTask} is a task object inside a L{Cooperator}, which can be
  309. paused, resumed, and stopped. It can also have its completion (or
  310. termination) monitored.
  311. @see: L{Cooperator.cooperate}
  312. @ivar _iterator: the iterator to iterate when this L{CooperativeTask} is
  313. asked to do work.
  314. @ivar _cooperator: the L{Cooperator} that this L{CooperativeTask}
  315. participates in, which is used to re-insert it upon resume.
  316. @ivar _deferreds: the list of L{Deferred}s to fire when this task
  317. completes, fails, or finishes.
  318. @ivar _pauseCount: the number of times that this L{CooperativeTask} has
  319. been paused; if 0, it is running.
  320. @ivar _completionState: The completion-state of this L{CooperativeTask}.
  321. L{None} if the task is not yet completed, an instance of L{TaskStopped}
  322. if C{stop} was called to stop this task early, of L{TaskFailed} if the
  323. application code in the iterator raised an exception which caused it to
  324. terminate, and of L{TaskDone} if it terminated normally via raising
  325. C{StopIteration}.
  326. """
  327. def __init__(
  328. self, iterator: Iterator[_TaskResultT], cooperator: "Cooperator"
  329. ) -> None:
  330. """
  331. A private constructor: to create a new L{CooperativeTask}, see
  332. L{Cooperator.cooperate}.
  333. """
  334. self._iterator = iterator
  335. self._cooperator = cooperator
  336. self._deferreds: List[Deferred[Iterator[_TaskResultT]]] = []
  337. self._pauseCount = 0
  338. self._completionState: Optional[SchedulerError] = None
  339. self._completionResult: Optional[Union[Iterator[_TaskResultT], Failure]] = None
  340. cooperator._addTask(self)
  341. def whenDone(self) -> Deferred[Iterator[_TaskResultT]]:
  342. """
  343. Get a L{Deferred} notification of when this task is complete.
  344. @return: a L{Deferred} that fires with the C{iterator} that this
  345. L{CooperativeTask} was created with when the iterator has been
  346. exhausted (i.e. its C{next} method has raised C{StopIteration}), or
  347. fails with the exception raised by C{next} if it raises some other
  348. exception.
  349. @rtype: L{Deferred}
  350. """
  351. d: Deferred[Iterator[_TaskResultT]] = Deferred()
  352. if self._completionState is None:
  353. self._deferreds.append(d)
  354. else:
  355. assert self._completionResult is not None
  356. d.callback(self._completionResult)
  357. return d
  358. def pause(self) -> None:
  359. """
  360. Pause this L{CooperativeTask}. Stop doing work until
  361. L{CooperativeTask.resume} is called. If C{pause} is called more than
  362. once, C{resume} must be called an equal number of times to resume this
  363. task.
  364. @raise TaskFinished: if this task has already finished or completed.
  365. """
  366. self._checkFinish()
  367. self._pauseCount += 1
  368. if self._pauseCount == 1:
  369. self._cooperator._removeTask(self)
  370. def resume(self) -> None:
  371. """
  372. Resume processing of a paused L{CooperativeTask}.
  373. @raise NotPaused: if this L{CooperativeTask} is not paused.
  374. """
  375. if self._pauseCount == 0:
  376. raise NotPaused()
  377. self._pauseCount -= 1
  378. if self._pauseCount == 0 and self._completionState is None:
  379. self._cooperator._addTask(self)
  380. def _completeWith(
  381. self,
  382. completionState: SchedulerError,
  383. deferredResult: Union[Iterator[_TaskResultT], Failure],
  384. ) -> None:
  385. """
  386. @param completionState: a L{SchedulerError} exception or a subclass
  387. thereof, indicating what exception should be raised when subsequent
  388. operations are performed.
  389. @param deferredResult: the result to fire all the deferreds with.
  390. """
  391. self._completionState = completionState
  392. self._completionResult = deferredResult
  393. if not self._pauseCount:
  394. self._cooperator._removeTask(self)
  395. # The Deferreds need to be invoked after all this is completed, because
  396. # a Deferred may want to manipulate other tasks in a Cooperator. For
  397. # example, if you call "stop()" on a cooperator in a callback on a
  398. # Deferred returned from whenDone(), this CooperativeTask must be gone
  399. # from the Cooperator by that point so that _completeWith is not
  400. # invoked reentrantly; that would cause these Deferreds to blow up with
  401. # an AlreadyCalledError, or the _removeTask to fail with a ValueError.
  402. for d in self._deferreds:
  403. d.callback(deferredResult)
  404. def stop(self) -> None:
  405. """
  406. Stop further processing of this task.
  407. @raise TaskFinished: if this L{CooperativeTask} has previously
  408. completed, via C{stop}, completion, or failure.
  409. """
  410. self._checkFinish()
  411. self._completeWith(TaskStopped(), Failure(TaskStopped()))
  412. def _checkFinish(self) -> None:
  413. """
  414. If this task has been stopped, raise the appropriate subclass of
  415. L{TaskFinished}.
  416. """
  417. if self._completionState is not None:
  418. raise self._completionState
  419. def _oneWorkUnit(self) -> None:
  420. """
  421. Perform one unit of work for this task, retrieving one item from its
  422. iterator, stopping if there are no further items in the iterator, and
  423. pausing if the result was a L{Deferred}.
  424. """
  425. try:
  426. result = next(self._iterator)
  427. except StopIteration:
  428. self._completeWith(TaskDone(), self._iterator)
  429. except BaseException:
  430. self._completeWith(TaskFailed(), Failure())
  431. else:
  432. if isinstance(result, Deferred):
  433. self.pause()
  434. def failLater(failure: Failure) -> None:
  435. self._completeWith(TaskFailed(), failure)
  436. result.addCallbacks(lambda result: self.resume(), failLater)
  437. class Cooperator:
  438. """
  439. Cooperative task scheduler.
  440. A cooperative task is an iterator where each iteration represents an
  441. atomic unit of work. When the iterator yields, it allows the
  442. L{Cooperator} to decide which of its tasks to execute next. If the
  443. iterator yields a L{Deferred} then work will pause until the
  444. L{Deferred} fires and completes its callback chain.
  445. When a L{Cooperator} has more than one task, it distributes work between
  446. all tasks.
  447. There are two ways to add tasks to a L{Cooperator}, L{cooperate} and
  448. L{coiterate}. L{cooperate} is the more useful of the two, as it returns a
  449. L{CooperativeTask}, which can be L{paused<CooperativeTask.pause>},
  450. L{resumed<CooperativeTask.resume>} and L{waited
  451. on<CooperativeTask.whenDone>}. L{coiterate} has the same effect, but
  452. returns only a L{Deferred} that fires when the task is done.
  453. L{Cooperator} can be used for many things, including but not limited to:
  454. - running one or more computationally intensive tasks without blocking
  455. - limiting parallelism by running a subset of the total tasks
  456. simultaneously
  457. - doing one thing, waiting for a L{Deferred} to fire,
  458. doing the next thing, repeat (i.e. serializing a sequence of
  459. asynchronous tasks)
  460. Multiple L{Cooperator}s do not cooperate with each other, so for most
  461. cases you should use the L{global cooperator<task.cooperate>}.
  462. """
  463. def __init__(
  464. self,
  465. terminationPredicateFactory: Callable[[], Callable[[], bool]] = _Timer,
  466. scheduler: Callable[[Callable[[], None]], IDelayedCall] = _defaultScheduler,
  467. started: bool = True,
  468. ):
  469. """
  470. Create a scheduler-like object to which iterators may be added.
  471. @param terminationPredicateFactory: A no-argument callable which will
  472. be invoked at the beginning of each step and should return a
  473. no-argument callable which will return True when the step should be
  474. terminated. The default factory is time-based and allows iterators to
  475. run for 1/100th of a second at a time.
  476. @param scheduler: A one-argument callable which takes a no-argument
  477. callable and should invoke it at some future point. This will be used
  478. to schedule each step of this Cooperator.
  479. @param started: A boolean which indicates whether iterators should be
  480. stepped as soon as they are added, or if they will be queued up until
  481. L{Cooperator.start} is called.
  482. """
  483. self._tasks: List[CooperativeTask] = []
  484. self._metarator: Iterator[CooperativeTask] = iter(())
  485. self._terminationPredicateFactory = terminationPredicateFactory
  486. self._scheduler = scheduler
  487. self._delayedCall: Optional[IDelayedCall] = None
  488. self._stopped = False
  489. self._started = started
  490. def coiterate(
  491. self,
  492. iterator: Iterator[_TaskResultT],
  493. doneDeferred: Optional[Deferred[Iterator[_TaskResultT]]] = None,
  494. ) -> Deferred[Iterator[_TaskResultT]]:
  495. """
  496. Add an iterator to the list of iterators this L{Cooperator} is
  497. currently running.
  498. Equivalent to L{cooperate}, but returns a L{Deferred} that will
  499. be fired when the task is done.
  500. @param doneDeferred: If specified, this will be the Deferred used as
  501. the completion deferred. It is suggested that you use the default,
  502. which creates a new Deferred for you.
  503. @return: a Deferred that will fire when the iterator finishes.
  504. """
  505. if doneDeferred is None:
  506. doneDeferred = Deferred()
  507. CooperativeTask(iterator, self).whenDone().chainDeferred(doneDeferred)
  508. return doneDeferred
  509. def cooperate(self, iterator: Iterator[_TaskResultT]) -> CooperativeTask:
  510. """
  511. Start running the given iterator as a long-running cooperative task, by
  512. calling next() on it as a periodic timed event.
  513. @param iterator: the iterator to invoke.
  514. @return: a L{CooperativeTask} object representing this task.
  515. """
  516. return CooperativeTask(iterator, self)
  517. def _addTask(self, task: CooperativeTask) -> None:
  518. """
  519. Add a L{CooperativeTask} object to this L{Cooperator}.
  520. """
  521. if self._stopped:
  522. self._tasks.append(task) # XXX silly, I know, but _completeWith
  523. # does the inverse
  524. task._completeWith(SchedulerStopped(), Failure(SchedulerStopped()))
  525. else:
  526. self._tasks.append(task)
  527. self._reschedule()
  528. def _removeTask(self, task: CooperativeTask) -> None:
  529. """
  530. Remove a L{CooperativeTask} from this L{Cooperator}.
  531. """
  532. self._tasks.remove(task)
  533. # If no work left to do, cancel the delayed call:
  534. if not self._tasks and self._delayedCall:
  535. self._delayedCall.cancel()
  536. self._delayedCall = None
  537. def _tasksWhileNotStopped(self) -> Iterable[CooperativeTask]:
  538. """
  539. Yield all L{CooperativeTask} objects in a loop as long as this
  540. L{Cooperator}'s termination condition has not been met.
  541. """
  542. terminator = self._terminationPredicateFactory()
  543. while self._tasks:
  544. for t in self._metarator:
  545. yield t
  546. if terminator():
  547. return
  548. self._metarator = iter(self._tasks)
  549. def _tick(self) -> None:
  550. """
  551. Run one scheduler tick.
  552. """
  553. self._delayedCall = None
  554. for taskObj in self._tasksWhileNotStopped():
  555. taskObj._oneWorkUnit()
  556. self._reschedule()
  557. _mustScheduleOnStart = False
  558. def _reschedule(self) -> None:
  559. if not self._started:
  560. self._mustScheduleOnStart = True
  561. return
  562. if self._delayedCall is None and self._tasks:
  563. self._delayedCall = self._scheduler(self._tick)
  564. def start(self) -> None:
  565. """
  566. Begin scheduling steps.
  567. """
  568. self._stopped = False
  569. self._started = True
  570. if self._mustScheduleOnStart:
  571. del self._mustScheduleOnStart
  572. self._reschedule()
  573. def stop(self) -> None:
  574. """
  575. Stop scheduling steps. Errback the completion Deferreds of all
  576. iterators which have been added and forget about them.
  577. """
  578. self._stopped = True
  579. for taskObj in self._tasks:
  580. taskObj._completeWith(SchedulerStopped(), Failure(SchedulerStopped()))
  581. self._tasks = []
  582. if self._delayedCall is not None:
  583. self._delayedCall.cancel()
  584. self._delayedCall = None
  585. @property
  586. def running(self) -> bool:
  587. """
  588. Is this L{Cooperator} is currently running?
  589. @return: C{True} if the L{Cooperator} is running, C{False} otherwise.
  590. @rtype: C{bool}
  591. """
  592. return self._started and not self._stopped
  593. _theCooperator = Cooperator()
  594. def coiterate(iterator: Iterator[_T]) -> Deferred[Iterator[_T]]:
  595. """
  596. Cooperatively iterate over the given iterator, dividing runtime between it
  597. and all other iterators which have been passed to this function and not yet
  598. exhausted.
  599. @param iterator: the iterator to invoke.
  600. @return: a Deferred that will fire when the iterator finishes.
  601. """
  602. return _theCooperator.coiterate(iterator)
  603. def cooperate(iterator: Iterator[_T]) -> CooperativeTask:
  604. """
  605. Start running the given iterator as a long-running cooperative task, by
  606. calling next() on it as a periodic timed event.
  607. This is very useful if you have computationally expensive tasks that you
  608. want to run without blocking the reactor. Just break each task up so that
  609. it yields frequently, pass it in here and the global L{Cooperator} will
  610. make sure work is distributed between them without blocking longer than a
  611. single iteration of a single task.
  612. @param iterator: the iterator to invoke.
  613. @return: a L{CooperativeTask} object representing this task.
  614. """
  615. return _theCooperator.cooperate(iterator)
  616. @implementer(IReactorTime)
  617. class Clock:
  618. """
  619. Provide a deterministic, easily-controlled implementation of
  620. L{IReactorTime.callLater}. This is commonly useful for writing
  621. deterministic unit tests for code which schedules events using this API.
  622. """
  623. rightNow = 0.0
  624. def __init__(self) -> None:
  625. self.calls: List[DelayedCall] = []
  626. def seconds(self) -> float:
  627. """
  628. Pretend to be time.time(). This is used internally when an operation
  629. such as L{IDelayedCall.reset} needs to determine a time value
  630. relative to the current time.
  631. @return: The time which should be considered the current time.
  632. """
  633. return self.rightNow
  634. def _sortCalls(self) -> None:
  635. """
  636. Sort the pending calls according to the time they are scheduled.
  637. """
  638. self.calls.sort(key=lambda a: a.getTime())
  639. def callLater(
  640. self, delay: float, callable: Callable[..., object], *args: object, **kw: object
  641. ) -> IDelayedCall:
  642. """
  643. See L{twisted.internet.interfaces.IReactorTime.callLater}.
  644. """
  645. dc = DelayedCall(
  646. self.seconds() + delay,
  647. callable,
  648. args,
  649. kw,
  650. self.calls.remove,
  651. lambda c: None,
  652. self.seconds,
  653. )
  654. self.calls.append(dc)
  655. self._sortCalls()
  656. return dc
  657. def getDelayedCalls(self) -> Sequence[IDelayedCall]:
  658. """
  659. See L{twisted.internet.interfaces.IReactorTime.getDelayedCalls}
  660. """
  661. return self.calls
  662. def advance(self, amount: float) -> None:
  663. """
  664. Move time on this clock forward by the given amount and run whatever
  665. pending calls should be run.
  666. @param amount: The number of seconds which to advance this clock's
  667. time.
  668. """
  669. self.rightNow += amount
  670. self._sortCalls()
  671. while self.calls and self.calls[0].getTime() <= self.seconds():
  672. call = self.calls.pop(0)
  673. call.called = 1
  674. call.func(*call.args, **call.kw)
  675. self._sortCalls()
  676. def pump(self, timings: Iterable[float]) -> None:
  677. """
  678. Advance incrementally by the given set of times.
  679. """
  680. for amount in timings:
  681. self.advance(amount)
  682. def deferLater(
  683. clock: IReactorTime,
  684. delay: float,
  685. callable: Optional[Callable[..., _T]] = None,
  686. *args: object,
  687. **kw: object,
  688. ) -> Deferred[_T]:
  689. """
  690. Call the given function after a certain period of time has passed.
  691. @param clock: The object which will be used to schedule the delayed
  692. call.
  693. @param delay: The number of seconds to wait before calling the function.
  694. @param callable: The callable to call after the delay, or C{None}.
  695. @param args: The positional arguments to pass to C{callable}.
  696. @param kw: The keyword arguments to pass to C{callable}.
  697. @return: A deferred that fires with the result of the callable when the
  698. specified time has elapsed.
  699. """
  700. def deferLaterCancel(deferred: Deferred[object]) -> None:
  701. delayedCall.cancel()
  702. def cb(result: object) -> _T:
  703. if callable is None:
  704. return None # type: ignore[return-value]
  705. return callable(*args, **kw)
  706. d: Deferred[_T] = Deferred(deferLaterCancel)
  707. d.addCallback(cb)
  708. delayedCall = clock.callLater(delay, d.callback, None)
  709. return d
  710. def react(
  711. main: Callable[
  712. ...,
  713. Union[Deferred[_T], Coroutine["Deferred[_T]", object, _T]],
  714. ],
  715. argv: Iterable[object] = (),
  716. _reactor: Optional[IReactorCore] = None,
  717. ) -> NoReturn:
  718. """
  719. Call C{main} and run the reactor until the L{Deferred} it returns fires or
  720. the coroutine it returns completes.
  721. This is intended as the way to start up an application with a well-defined
  722. completion condition. Use it to write clients or one-off asynchronous
  723. operations. Prefer this to calling C{reactor.run} directly, as this
  724. function will also:
  725. - Take care to call C{reactor.stop} once and only once, and at the right
  726. time.
  727. - Log any failures from the C{Deferred} returned by C{main}.
  728. - Exit the application when done, with exit code 0 in case of success and
  729. 1 in case of failure. If C{main} fails with a C{SystemExit} error, the
  730. code returned is used.
  731. The following demonstrates the signature of a C{main} function which can be
  732. used with L{react}::
  733. async def main(reactor, username, password):
  734. return "ok"
  735. task.react(main, ("alice", "secret"))
  736. @param main: A callable which returns a L{Deferred} or
  737. coroutine. It should take the reactor as its first
  738. parameter, followed by the elements of C{argv}.
  739. @param argv: A list of arguments to pass to C{main}. If omitted the
  740. callable will be invoked with no additional arguments.
  741. @param _reactor: An implementation detail to allow easier unit testing. Do
  742. not supply this parameter.
  743. @since: 12.3
  744. """
  745. if _reactor is None:
  746. from twisted.internet import reactor
  747. _reactor = cast(IReactorCore, reactor)
  748. finished = ensureDeferred(main(_reactor, *argv))
  749. code = 0
  750. stopping = False
  751. def onShutdown() -> None:
  752. nonlocal stopping
  753. stopping = True
  754. _reactor.addSystemEventTrigger("before", "shutdown", onShutdown)
  755. def stop(result: object, stopReactor: bool) -> None:
  756. if stopReactor:
  757. assert _reactor is not None
  758. try:
  759. _reactor.stop()
  760. except ReactorNotRunning:
  761. pass
  762. if isinstance(result, Failure):
  763. nonlocal code
  764. if result.check(SystemExit) is not None:
  765. code = result.value.code
  766. else:
  767. log.err(result, "main function encountered error")
  768. code = 1
  769. def cbFinish(result: object) -> None:
  770. if stopping:
  771. stop(result, False)
  772. else:
  773. assert _reactor is not None
  774. _reactor.callWhenRunning(stop, result, True)
  775. finished.addBoth(cbFinish)
  776. _reactor.run()
  777. sys.exit(code)
  778. __all__ = [
  779. "LoopingCall",
  780. "Clock",
  781. "SchedulerStopped",
  782. "Cooperator",
  783. "coiterate",
  784. "deferLater",
  785. "react",
  786. ]