daily pastebin goal
43%
SHARE
TWEET

Untitled

a guest Nov 24th, 2017 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from twisted.application import service, internet
  2. from twisted.internet import defer, task, reactor
  3.  
  4. def sleep(t):
  5.     return task.deferLater(reactor, t, lambda: None)
  6.  
  7. class WorkerApp(service.Service):
  8.     myd = None
  9.  
  10.     def __init__(self, reactor):
  11.         self._reactor = reactor
  12.         print("workerapp init")
  13.  
  14.     async def _startService(self):
  15.         self.running = True
  16.         self.myd = defer.Deferred()
  17.  
  18.         while self.running:
  19.             self.myd = defer.Deferred()
  20.             try:
  21.                 print("1")
  22.                 await sleep(2)
  23.                 #raise Exception('my exception')
  24.                 print("2")
  25.                 await sleep(2)
  26.                 print("3")
  27.                 await sleep(2)
  28.             except:
  29.                 # Put logging here
  30.                 self.running = False
  31.                 self.myd.callback(self)
  32.                 self._reactor.stop()
  33.             else:
  34.                 self.myd.callback(self)
  35.  
  36.     def startService(self):
  37.         defer.ensureDeferred(self._startService())
  38.  
  39.     async def _stopService(self):
  40.         print("stop service")
  41.         #try:
  42.         #    await sleep(1)
  43.         #except Exception as e:
  44.         #    raise
  45.         self.myd.addCallback(setattr, "running", False)
  46.         return self.myd
  47.  
  48.     def stopService(self):
  49.        defer.ensureDeferred(self._stopService())
  50.        #return self._stopService()
  51.  
  52.  
  53. application = service.Application("my twisted app")
  54. service = WorkerApp(reactor)
  55. service.setServiceParent(application)
RAW Paste Data
Top