Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active June 13, 2017 03:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save njsmith/634b596e5765d5ed8b819a4f8e56d306 to your computer and use it in GitHub Desktop.
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)
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)
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