Last active
June 13, 2017 03:34
-
-
Save njsmith/634b596e5765d5ed8b819a4f8e56d306 to your computer and use it in GitHub Desktop.
MultiError examples from my 2017 python language summit talk (slides: https://vorpus.org/~njs/misc/trio-language-summit-2017.pdf)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trio | |
async def main(): | |
async with trio.open_nursery() as nursery: | |
nursery.spawn(f1) | |
nursery.spawn(f2) | |
async def f1(): | |
raise KeyError | |
async def f2(): | |
async with trio.open_nursery() as nursery: | |
nursery.spawn(f2a) | |
nursery.spawn(f2b) | |
async def f2a(): | |
raise ValueError | |
async def f2b(): | |
raise RuntimeError | |
trio.run(main) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import trio | |
async def main(): | |
def handler(exc): | |
if isinstance(exc, (RuntimeError, KeyError)): | |
return None | |
else: | |
return exc | |
with trio.MultiError.catch(handler): | |
async with trio.open_nursery() as nursery: | |
nursery.spawn(f1) | |
nursery.spawn(f2) | |
async def f1(): | |
raise KeyError | |
async def f2(): | |
async with trio.open_nursery() as nursery: | |
nursery.spawn(f2a) | |
nursery.spawn(f2b) | |
async def f2a(): | |
raise ValueError | |
async def f2b(): | |
raise RuntimeError | |
trio.run(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment