Asynchronous exception handling in Python

前端 未结 2 1657
野的像风
野的像风 2020-12-12 22:07

I\'ve the following code using asyncio and aiohttp to make asynchronous HTTP requests.

import sys
import asyncio
import aiohttp

@a         


        
2条回答
  •  Happy的楠姐
    2020-12-12 22:51

    To debug or "handle" exceptions in callback:

    Coroutine which return some result or raise exceptions:

    @asyncio.coroutine
    def async_something_entry_point(self):
        try:
            return self.real_stuff_which_throw_exceptions()
        except:
            raise Exception(some_identifier_here + ' ' + traceback.format_exc())
    

    And callback:

    def callback(self, future: asyncio.Future):
        exc = future.exception()
        if exc:
            # Handle wonderful empty TimeoutError exception
            if type(exc) == TimeoutError:
                self.logger(' callback exception TimeoutError')
            else:
                self.logger(" callback exception " + str(exc))
    
        # store your result where you want
        self.result.append(
            future.result()
        )
    

提交回复
热议问题