Author lilydjwg
Recipients asvetlov, lilydjwg, yselivanov
Date 2020-12-19.08:09:24
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1608365364.67.0.620283086401.issue42682@roundup.psfhosted.org>
In-reply-to
Content
import asyncio

async def crash(key):
  raise Exception('crash!')

async def wait(fu):
  await fu

async def main():
  crasher = asyncio.create_task(crash(()))
  fs = [wait(crasher) for _ in range(10)]
  for fu in asyncio.as_completed(fs):
    try:
      await fu
    except Exception:
      import traceback
      traceback.print_exc()

if __name__ == '__main__':
  asyncio.run(main())

This code will give a very long traceback 10 times. I expect it to be short.

I'm caching the result of an async function like this:

  async def get(
    self,
    key: Hashable,
    func: Callable[[Hashable], Coroutine[Any, Any, Any]],
  ) -> Any:
    async with self.lock:
      cached = self.cache.get(key)
      if cached is None:
        coro = func(key)
        fu = asyncio.create_task(coro)
        self.cache[key] = fu

    if asyncio.isfuture(cached): # pending
      return await cached # type: ignore
    elif cached is not None: # cached
      return cached
    else: # not cached
      r = await fu
      self.cache[key] = r
      return r

It works fine, except that when there is an exception the traceback is very long.
History
Date User Action Args
2020-12-19 08:09:24lilydjwgsetrecipients: + lilydjwg, asvetlov, yselivanov
2020-12-19 08:09:24lilydjwgsetmessageid: <1608365364.67.0.620283086401.issue42682@roundup.psfhosted.org>
2020-12-19 08:09:24lilydjwglinkissue42682 messages
2020-12-19 08:09:24lilydjwgcreate