task.py 928 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #!/usr/bin/env python
  2. # https://twistedmatrix.com/documents/16.2.0/core/howto/defer.html
  3. # task has no problem with stop() and .start()
  4. # Why would it be getting lost in my code, I wonder? >:(
  5. from twisted.internet import reactor, defer, task
  6. from pprint import pprint
  7. import pendulum
  8. def now():
  9. return pendulum.now().to_datetime_string()
  10. def awake():
  11. print("awake", now())
  12. def startTask(d):
  13. global t
  14. print("task awake, {0}".format(d), now())
  15. t = task.LoopingCall(awake)
  16. t.start(d)
  17. def stopTask(d):
  18. global t
  19. print("STOP!", now())
  20. t.stop()
  21. reactor.callLater(d*2 + 1, startTask, d)
  22. d = 2
  23. reactor.callLater(1, startTask, d)
  24. reactor.callLater(d * 3 + 1, stopTask, d + 1)
  25. print("Stop the reactor in 40 seconds...", now())
  26. reactor.callLater(40, reactor.stop)
  27. print("starting the reactor", now())
  28. reactor.run()
  29. print("Done.", now())
  30. print("Reactor has been safely shutdown. :P")