Reducer for similar simultaneously coroutines
Project description
About Async-Reduce
async_reduce(coroutine)
allows aggregate all similar simultaneous ready
to run coroutine
s and reduce to running only one coroutine
.
Other aggregated coroutine
s will get result from single coroutine
.
It can boost application performance in highly competitive execution of the similar asynchronous operations and reduce load for inner systems.
Quick example
from async_reduce import async_reduce
async def fetch_user_data(user_id: int) -> dict:
"""" Get user data from inner service """
url = 'http://inner-service/user/{}'.format(user_id)
return await http.get(url, timeout=10).json()
@web_server.router('/users/(\d+)')
async def handler_user_detail(request, user_id: int):
""" Handler for get detail information about user """
# all simultaneous requests of fetching user data for `user_id` will
# reduced to single request
user_data = await async_reduce(
fetch_user_data(user_id)
)
# sometimes ``async_reduce`` cannot detect similar coroutines and
# you should provide special argument `ident` for manually determination
user_statistics = await async_reduce(
DataBase.query('user_statistics').where(id=user_id).fetch_one(),
ident='db_user_statistics:{}'.format(user_id)
)
return Response(...)
In that example without using async_reduce
if client performs N
simultaneous requests like GET http://web_server/users/42
web_server
performs N requests to inner-service and N queries to database.
In total: N simultaneous requests emits 2 * N requests to inner systems.
With async_reduce
if client performs N simultaneous requests web_server
performs one requests to inner-service and one queries to database.
In total: N simultaneous requests emits only 2 requests to inner systems.
See other real examples.
Similar coroutines determination
async_reduce(coroutine)
tries detect similar coroutines by hashing local
variables bounded on call. It's not working correctly when:
- one of arguments is not hashable
- coroutine function is method of class with specific state (like ORM)
- coroutine function has closure to unhashable variable
You can disable auto-determination by set custom key to argument ident
.
Use as decorator
Also library provide special decorator @async_reduceable()
, example:
from async_reduce import async_reduceable
@async_reduceable()
async def fetch_user_data(user_id: int) -> dict:
"""" Get user data from inner service """
url = 'http://inner-servicce/user/{}'.format(user_id)
return await http.get(url, timeout=10).json()
@web_server.router('/users/(\d+)')
async def handler_user_detail(request, user_id: int):
""" Handler for get detail information about user """
return await fetch_user_data(user_id)
Caveats
-
If single
coroutine
raises exceptions all aggregatedcoroutine
s will get same exception too -
If single
coroutine
is stuck all aggregatedcoroutine
s will stuck too. Limit execution time forcoroutine
and add retries (optional) to avoid it.
Development
See DEVELOPMENT.md.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for async_reduce-0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcdac61981dfb9f22beb8c2f77c607fa4f85f38356faef6c359ffed80b27508d |
|
MD5 | cb589ce06144cad23ffbf901fa242fb3 |
|
BLAKE2b-256 | 42660d39cffb4a44d61b948834b3f96867ff7e92ee680884b8f5cc347162b405 |