Advertisement
Guest User

Untitled

a guest
Nov 21st, 2017
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. import time
  2. from twisted.application import service, internet
  3. from twisted.internet import defer, task, reactor
  4.  
  5. def sleep(t):
  6. return task.deferLater(reactor, t, lambda: None)
  7.  
  8. class WorkerApp(service.Service):
  9. myd = None
  10. def __init__(self):
  11. print("workerapp init")
  12.  
  13. async def _startService(self):
  14. self.running = True
  15. while self.running:
  16. self.myd = defer.Deferred()
  17. print("1")
  18. await sleep(2)
  19. print("2")
  20. await sleep(2)
  21. raise Exception('my error')
  22. print("3")
  23. await sleep(2)
  24. self.myd.callback(self)
  25.  
  26. def startService(self):
  27. defer.ensureDeferred(self._startService())
  28.  
  29. def stopService(self):
  30. print("stop service")
  31. self.myd.addCallback(setattr, "running", False)
  32. return self.myd
  33.  
  34. application = service.Application("my twisted app")
  35. service = WorkerApp()
  36. service.setServiceParent(application)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement