-
Book Overview & Buying
-
Table Of Contents
-
Feedback & Rating
Asynchronous Programming in Python
By :
The basic rules of Python exception handling in synchronous code are identical in asynchronous implementations, but you must decide whether to handle exceptions locally in the coroutine/task or propagate them up to the caller.In previous implementations you can see that a really generic behavior is implemented in the get_data_nonblocking method: if a aiohttp.ClientResponseError is thrown then the result is a string with the message of the error, which breaks the method contract (a dict is expected as result) and the returned message is uninformative. Similarly, the get_data method handles all possible exceptions by just returning them as a string, which is also not the best way to report the anomaly. The following code shows an alternative implementation which offers some improvements:
import asyncio
import aiohttp
BASE_URL = https://ponyapi.net/v1/character/
def get_data(person):
try: p = person["data"][0]
return f'{p[...