twisted.application.internet.TimerService - python examples

Here are the examples of the python api twisted.application.internet.TimerService taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

17 Examples 7

3 View Complete Implementation : _twistw.py
Copyright MIT License
Author : adde88
    def postApplication(self):
        """
        Start the application and run the reactor.
        """
        service.IService(self.application).privilegedStartService()
        app.startApplication(self.application, not self.config['no_save'])
        app.startApplication(internet.TimerService(0.1, lambda:None), 0)
        app.runReactorWithLogging(self.config, self.oldstdout, self.oldstderr)
        app.reportProfile(self.config['report-profile'],
                          service.IProcess(self.application).processName)
        log.msg("Server Shut Down.")

3 View Complete Implementation : test_application.py
Copyright MIT License
Author : adde88
    def testPickledTimer(self):
        target = TimerTarget()
        t0 = internet.TimerService(1, target.append, "hello")
        t0.startService()
        s = pickle.dumps(t0)
        t0.stopService()

        t = pickle.loads(s)
        self.failIf(t.running)

3 View Complete Implementation : test_application.py
Copyright MIT License
Author : adde88
    def testBrokenTimer(self):
        d = defer.Deferred()
        t = internet.TimerService(1, lambda: 1 / 0)
        oldFailed = t._failed
        def _failed(why):
            oldFailed(why)
            d.callback(None)
        t._failed = _failed
        t.startService()
        d.addCallback(lambda x : t.stopService)
        d.addCallback(lambda x : self.astertEqual(
            [ZeroDivisionError],
            [o.value.__clast__ for o in log.flushErrors(ZeroDivisionError)]))
        return d

3 View Complete Implementation : test_application.py
Copyright MIT License
Author : adde88
    def testTimerRuns(self):
        d = defer.Deferred()
        self.t = internet.TimerService(1, d.callback, 'hello')
        self.t.startService()
        d.addCallback(self.astertEqual, 'hello')
        d.addCallback(lambda x : self.t.stopService())
        d.addCallback(lambda x : self.failIf(self.t.running))
        return d

3 View Complete Implementation : test_application.py
Copyright MIT License
Author : adde88
    def testTimerLoops(self):
        l = []
        def trigger(data, number, d):
            l.append(data)
            if len(l) == number:
                d.callback(l)
        d = defer.Deferred()
        self.t = internet.TimerService(0.01, trigger, "hello", 10, d)
        self.t.startService()
        d.addCallback(self.astertEqual, ['hello'] * 10)
        d.addCallback(lambda x : self.t.stopService())
        return d

3 View Complete Implementation : tftp.py
Copyright GNU Affero General Public License v3.0
Author : maas
    def __init__(self, resource_root, port, client_service):
        """
        :param resource_root: The root directory for this TFTP server.
        :param port: The port on which each server should be started.
        :param client_service: The RPC client service for the rack controller.
        """
        super(TFTPService, self).__init__()
        self.backend = TFTPBackend(resource_root, client_service)
        self.port = port
        # Establish a periodic call to self.updateServers() every 45
        # seconds, so that this service eventually converges on truth.
        # TimerService ensures that a call is made to it's target
        # function immediately as it's started, so there's no need to
        # call updateServers() from here.
        self.refresher = internet.TimerService(45, self.updateServers)
        self.refresher.setName("refresher")
        self.refresher.setServiceParent(self)

3 View Complete Implementation : service.py
Copyright MIT License
Author : twisted
    def startService(self):
        Service.startService(self)
        self._registered = False
        self._timer_service = TimerService(
            self.check_interval.total_seconds(), self._check_certs)
        self._timer_service.clock = self._clock
        self._timer_service.startService()

3 View Complete Implementation : test_internet.py
Copyright MIT License
Author : wistbean
    def setUp(self):
        """
        Set up a timer service to test.
        """
        self.timer = TimerService(2, self.call)
        self.clock = self.timer.clock = task.Clock()
        self.deferred = Deferred()

3 View Complete Implementation : test_internet.py
Copyright MIT License
Author : wistbean
    def test_pickleTimerServiceNotPickleLoop(self):
        """
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loop}.
        """
        # We need a pickleable callable to test pickling TimerService. So we
        # can't use self.timer
        timer = TimerService(1, fakeTargetFunction)
        timer.startService()
        dumpedTimer = pickle.dumps(timer)
        timer.stopService()
        loadedTimer = pickle.loads(dumpedTimer)
        nothing = object()
        value = getattr(loadedTimer, "_loop", nothing)
        self.astertIdentical(nothing, value)

3 View Complete Implementation : test_internet.py
Copyright MIT License
Author : wistbean
    def test_pickleTimerServiceNotPickleLoopFinished(self):
        """
        When pickling L{internet.TimerService}, it won't pickle
        L{internet.TimerService._loopFinished}.
        """
        # We need a pickleable callable to test pickling TimerService. So we
        # can't use self.timer
        timer = TimerService(1, fakeTargetFunction)
        timer.startService()
        dumpedTimer = pickle.dumps(timer)
        timer.stopService()
        loadedTimer = pickle.loads(dumpedTimer)
        nothing = object()
        value = getattr(loadedTimer, "_loopFinished", nothing)
        self.astertIdentical(nothing, value)