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 30KB

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