Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/piccolo-orm/asyncio_tools
Useful utilities for working with asyncio - including an asyncio.gather wrapper.
https://github.com/piccolo-orm/asyncio_tools
asyncio concurrency gather hacktoberfest python
Last synced: about 2 months ago
JSON representation
Useful utilities for working with asyncio - including an asyncio.gather wrapper.
- Host: GitHub
- URL: https://github.com/piccolo-orm/asyncio_tools
- Owner: piccolo-orm
- License: mit
- Created: 2020-02-24T19:04:57.000Z (almost 5 years ago)
- Default Branch: master
- Last Pushed: 2022-12-26T23:06:09.000Z (about 2 years ago)
- Last Synced: 2024-10-14T06:52:07.216Z (3 months ago)
- Topics: asyncio, concurrency, gather, hacktoberfest, python
- Language: Python
- Homepage:
- Size: 23.4 KB
- Stars: 14
- Watchers: 3
- Forks: 3
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGES
- License: LICENSE
Awesome Lists containing this project
README
# asyncio_tools
Useful utilities for working with asyncio.
## Installation
```
pip install asyncio-tools
```## gather
Provides a convenient wrapper around `asyncio.gather`.
```python
from asyncio_tools import gather, CompoundExceptionasync def good():
return 'OK'async def bad():
raise ValueError()async def main():
response = await gather(
good(),
bad(),
good()
)# Check if a particular exception was raised.
ValueError in response.exception_types
# >>> True# To get all exceptions:
print(response.exceptions)
# >>> [ValueError()]# To get all instances of a particular exception:
response.exceptions_of_type(ValueError)
# >>> [ValueError()]# To get the number of exceptions:
print(response.exception_count)
# >>> 1# You can still access all of the results:
print(response.all)
# >>> ['OK', ValueError(), 'OK']# And can access all successes (i.e. non-exceptions):
print(response.successes)
# >>> ['OK', 'OK']# To get the number of successes:
print(response.success_count)
# >>> 2try:
# To combines all of the exceptions into a single one, which merges the
# messages.
raise response.compound_exception()
except CompoundException as compound_exception:
print("Caught it")if ValueError in compound_exception.exception_types:
print("Caught a ValueError")```
Read some background on why `gather` is useful:
- https://www.piccolo-orm.com/blog/exception-handling-in-asyncio/
- https://www.piccolo-orm.com/blog/asyncio-gather/