Guest User

Untitled

a guest
Jan 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.39 KB | None | 0 0
  1.  
  2.  
  3. # poll mechanics
  4.  
  5. def poll(self, condition_func, poll_func, timeout_secs):
  6. now = datetime.datetime.now()
  7. timeout = now + datetime.timedelta(seconds=timeout_secs)
  8. # define generator closure
  9. def _poll():
  10. now = datetime.datetime.now()
  11. if now > timeout:
  12. raise RuntimeError('%s: poll timeout' % self)
  13. d = deferLater(reactor, 2, poll_func)
  14. # condition_func must raise StopIteration when done
  15. d.addCallback(condition_func)
  16. def _handle_exc(failure):
  17. if failure.type == StopIteration:
  18. failure.raiseException()
  19. else:
  20. return failure
  21. d.addErrback(_handle_exc)
  22. yield d
  23. # create a polling task
  24. task = cooperate(_poll())
  25. d = task.whenDone()
  26. d.addCallback(lambda _: self)
  27. return d
  28.  
  29. # set up polling task
  30.  
  31. def _pollState(self, ignore, context):
  32. poll_func = functools.partial(self.refresh, None, context)
  33. def condition_func(result):
  34. state = result['state']
  35. log.debug('%s: state (%s)' % (self, state))
  36. if state == 'running':
  37. raise StopIteration
  38. return state
  39. d = self.poll(condition_func, poll_func, 30)
  40. return d
Add Comment
Please, Sign In to add comment