/demo/delegation.py

http://github.com/sampsyo/bluelet · Python · 36 lines · 34 code · 0 blank · 2 comment · 1 complexity · 6c0bbbcabb86dbec7d8de00569e52a1c MD5 · raw file

  1. """A demonstration of Bluelet's approach to invoking (delegating to)
  2. sub-coroutines and spawning child coroutines.
  3. """
  4. import sys
  5. sys.path.insert(0, '..')
  6. import bluelet
  7. def child():
  8. print 'Child started.'
  9. yield bluelet.null()
  10. print 'Child resumed.'
  11. yield bluelet.null()
  12. print 'Child ending.'
  13. yield bluelet.end(42)
  14. def parent():
  15. print 'Parent started.'
  16. yield bluelet.null()
  17. print 'Parent resumed.'
  18. result = yield child()
  19. print 'Child returned:', repr(result)
  20. print 'Parent ending.'
  21. def exc_child():
  22. yield bluelet.null()
  23. raise Exception()
  24. def exc_parent():
  25. try:
  26. yield exc_child()
  27. except Exception, exc:
  28. print 'Parent caught:', repr(exc)
  29. def exc_grandparent():
  30. yield bluelet.spawn(exc_parent())
  31. if __name__ == '__main__':
  32. bluelet.run(parent())
  33. bluelet.run(exc_grandparent())