本文整理汇总了Python中twisted.application.internet.TimerService方法的典型用法代码示例。如果您正苦于以下问题:Python internet.TimerService方法的具体用法?Python internet.TimerService怎么用?Python internet.TimerService使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类twisted.application.internet
的用法示例。
在下文中一共展示了internet.TimerService方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: testTimerRestart
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def testTimerRestart(self):
# restart the same TimerService
d1 = defer.Deferred()
d2 = defer.Deferred()
work = [(d2, "bar"), (d1, "foo")]
def trigger():
d, arg = work.pop()
d.callback(arg)
self.t = internet.TimerService(1, trigger)
self.t.startService()
def onFirstResult(result):
self.assertEqual(result, 'foo')
return self.t.stopService()
def onFirstStop(ignored):
self.assertFalse(self.t.running)
self.t.startService()
return d2
def onSecondResult(result):
self.assertEqual(result, 'bar')
self.t.stopService()
d1.addCallback(onFirstResult)
d1.addCallback(onFirstStop)
d1.addCallback(onSecondResult)
return d1
示例2: testTimerRestart
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def testTimerRestart(self):
# restart the same TimerService
d1 = defer.Deferred()
d2 = defer.Deferred()
work = [(d2, "bar"), (d1, "foo")]
def trigger():
d, arg = work.pop()
d.callback(arg)
self.t = internet.TimerService(1, trigger)
self.t.startService()
def onFirstResult(result):
self.assertEqual(result, 'foo')
return self.t.stopService()
def onFirstStop(ignored):
self.failIf(self.t.running)
self.t.startService()
return d2
def onSecondResult(result):
self.assertEqual(result, 'bar')
self.t.stopService()
d1.addCallback(onFirstResult)
d1.addCallback(onFirstStop)
d1.addCallback(onSecondResult)
return d1
示例3: runApp
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def runApp(config):
passphrase = app.getPassphrase(config['encrypted'])
app.installReactor(config['reactor'])
application = app.getApplication(config, passphrase)
oldstdout = sys.stdout
oldstderr = sys.stderr
startLogging(config['logfile'])
app.initialLog()
os.chdir(config['rundir'])
service.IService(application).privilegedStartService()
app.startApplication(application, not config['no_save'])
app.startApplication(internet.TimerService(0.1, lambda:None), 0)
app.runReactorWithLogging(config, oldstdout, oldstderr)
app.reportProfile(config['report-profile'],
service.IProcess(application).processName)
log.msg("Server Shut Down.")
示例4: __init__
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
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().__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)
示例5: fakeTargetFunction
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def fakeTargetFunction():
"""
A fake target function for testing TimerService which does nothing.
"""
pass
示例6: setUp
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
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()
示例7: call
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def call(self):
"""
Function called by L{TimerService} being tested.
@returns: C{self.deferred}
@rtype: L{Deferred}
"""
return self.deferred
示例8: test_startService
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def test_startService(self):
"""
When L{TimerService.startService} is called, it marks itself
as running, creates a L{task.LoopingCall} and starts it.
"""
self.timer.startService()
self.assertTrue(self.timer.running, "Service is started")
self.assertIsInstance(self.timer._loop, task.LoopingCall)
self.assertIdentical(self.clock, self.timer._loop.clock)
self.assertTrue(self.timer._loop.running, "LoopingCall is started")
示例9: test_startServiceRunsCallImmediately
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def test_startServiceRunsCallImmediately(self):
"""
When L{TimerService.startService} is called, it calls the function
immediately.
"""
result = []
self.timer.call = (result.append, (None,), {})
self.timer.startService()
self.assertEqual([None], result)
示例10: test_stopServiceWaits
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def test_stopServiceWaits(self):
"""
When L{TimerService.stopService} is called while a call is in progress.
the L{Deferred} returned doesn't fire until after the call finishes.
"""
self.timer.startService()
d = self.timer.stopService()
self.assertNoResult(d)
self.assertEqual(True, self.timer.running)
self.deferred.callback(object())
self.assertIdentical(self.successResultOf(d), None)
示例11: test_stopServiceImmediately
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def test_stopServiceImmediately(self):
"""
When L{TimerService.stopService} is called while a call isn't in progress.
the L{Deferred} returned has already been fired.
"""
self.timer.startService()
self.deferred.callback(object())
d = self.timer.stopService()
self.assertIdentical(self.successResultOf(d), None)
示例12: test_failedCallLogsError
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
def test_failedCallLogsError(self):
"""
When function passed to L{TimerService} returns a deferred that errbacks,
the exception is logged, and L{TimerService.stopService} doesn't raise an error.
"""
self.timer.startService()
self.deferred.errback(Failure(ZeroDivisionError()))
errors = self.flushLoggedErrors(ZeroDivisionError)
self.assertEqual(1, len(errors))
d = self.timer.stopService()
self.assertIdentical(self.successResultOf(d), None)
示例13: test_pickleTimerServiceNotPickleLoop
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
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.assertIdentical(nothing, value)
示例14: test_pickleTimerServiceNotPickleLoopFinished
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
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.assertIdentical(nothing, value)
示例15: testPickledTimer
# 需要导入模块: from twisted.application import internet [as 别名]
# 或者: from twisted.application.internet import TimerService [as 别名]
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.assertFalse(t.running)