Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from twisted.application import service, internet
- from twisted.internet import defer, task, reactor
- def sleep(t):
- return task.deferLater(reactor, t, lambda: None)
- class WorkerApp(service.Service):
- myd = None
- def __init__(self):
- print("workerapp init")
- async def _startService(self):
- self.running = True
- while self.running:
- self.myd = defer.Deferred()
- print("1")
- await sleep(2)
- print("2")
- await sleep(2)
- raise Exception('my error')
- print("3")
- await sleep(2)
- self.myd.callback(self)
- def startService(self):
- defer.ensureDeferred(self._startService())
- def stopService(self):
- print("stop service")
- self.myd.addCallback(setattr, "running", False)
- return self.myd
- application = service.Application("my twisted app")
- service = WorkerApp()
- service.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement