PageRenderTime 20ms CodeModel.GetById 17ms app.highlight 2ms RepoModel.GetById 0ms app.codeStats 0ms

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