~certify-web-dev/twisted/certify-trunk

« back to all changes in this revision

Viewing changes to twisted/internet/task.py

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2007-01-17 14:52:35 UTC
  • mfrom: (1.1.5 upstream) (2.1.2 etch)
  • Revision ID: james.westby@ubuntu.com-20070117145235-btmig6qfmqfen0om
Tags: 2.5.0-0ubuntu1
New upstream version, compatible with python2.5.

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
from twisted.python.runtime import seconds
18
18
from twisted.python import reflect
19
19
 
20
 
from twisted.internet import defer
 
20
from twisted.internet import base, defer
21
21
 
22
22
 
23
23
class LoopingCall:
295
295
 
296
296
 
297
297
 
 
298
class Clock:
 
299
    """
 
300
    Provide a deterministic, easily-controlled implementation of
 
301
    L{IReactorTime.callLater}.  This is commonly useful for writing
 
302
    deterministic unit tests for code which schedules events using this API.
 
303
    """
 
304
    rightNow = 0.0
 
305
 
 
306
    def __init__(self):
 
307
        self.calls = []
 
308
 
 
309
    def seconds(self):
 
310
        """
 
311
        Pretend to be time.time().  This is used internally when an operation
 
312
        such as L{IDelayedCall.reset} needs to determine a a time value
 
313
        relative to the current time.
 
314
 
 
315
        @rtype: C{float}
 
316
        @return: The time which should be considered the current time.
 
317
        """
 
318
        return self.rightNow
 
319
 
 
320
 
 
321
    def callLater(self, when, what, *a, **kw):
 
322
        """
 
323
        See L{twisted.internet.interfaces.IReactorTime.callLater}.
 
324
        """
 
325
        self.calls.append(
 
326
            base.DelayedCall(self.seconds() + when,
 
327
                             what, a, kw,
 
328
                             self.calls.remove,
 
329
                             lambda c: None,
 
330
                             self.seconds))
 
331
        self.calls.sort(lambda a, b: cmp(a.getTime(), b.getTime()))
 
332
        return self.calls[-1]
 
333
 
 
334
 
 
335
    def advance(self, amount):
 
336
        """
 
337
        Move time on this clock forward by the given amount and run whatever
 
338
        pending calls should be run.
 
339
 
 
340
        @type amount: C{float}
 
341
        @param amount: The number of seconds which to advance this clock's
 
342
        time.
 
343
        """
 
344
        self.rightNow += amount
 
345
        while self.calls and self.calls[0].getTime() <= self.seconds():
 
346
            call = self.calls.pop(0)
 
347
            call.called = 1
 
348
            call.func(*call.args, **call.kw)
 
349
 
 
350
 
 
351
    def pump(self, timings):
 
352
        """
 
353
        Advance incrementally by the given set of times.
 
354
 
 
355
        @type timings: iterable of C{float}
 
356
        """
 
357
        for amount in timings:
 
358
            self.advance(amount)
 
359
 
 
360
 
 
361
 
298
362
__all__ = [
299
363
    'LoopingCall',
300
364
 
 
365
    'Clock',
 
366
 
301
367
    'SchedulerStopped', 'Cooperator', 'coiterate',
302
368
    ]