You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
There are many libraries / frameworks that try to be polyglottal by nature and support both sync python and asyncio-based Python. One of the key ways libraries do this is by using python stdlib functions like inspect.isawaitable or asyncio.iscoroutinefunction or similar in order to test if a function is a coroutine or not, and then treat it appropriately (e.g. by running it in the event loop or in a threadpool)
The problem is that redis-py's asyncio system does not use async functions, it uses "normal" functions that return coroutines. This effectively breaks everyone else's ability to treat asyncio redis methods appropriately.
Simple repro
importasyncioimportinspectfromredisimportasyncioasAsyncRedisredis_instance=AsyncRedis.from_url(REDIS_URL, health_check_interval=30, encoding="utf-8")
# returns False, should be Trueprint(asyncio.iscoroutinefunction(redis_instance.delete))
# also returns False but realistically should be Trueprint(inspect.isawaitable(redis_instance.delete))
# example of why this mattersasyncdefutility_function(func, *args, **kwargs):
ifasyncio.iscoroutinefunction(func):
awaitfunc(*args, **kwargs)
else:
awaitasyncio.to_thread(func, *args, **kwargs)
# does the wrong thing, coroutine is never awaitedawaitutility_function(redis_instance.delete, "card-cache:9f3faff0-9001-4a06-98cd-eaf1439861c5")
This is a fairly common pattern in other libs. Is there anything that can be done about this so that async functions are properly marked as such?
The text was updated successfully, but these errors were encountered:
acnebs
changed the title
Structure of redis.asyncio does not allow normal asyncio inspection
Structure of redis.asyncio does not allow asyncio coroutine inspection
Feb 27, 2024
Version: redis-py 5.0.1
Platform: Python 3.11 macOS
Description
There are many libraries / frameworks that try to be polyglottal by nature and support both sync python and asyncio-based Python. One of the key ways libraries do this is by using python stdlib functions like
inspect.isawaitable
orasyncio.iscoroutinefunction
or similar in order to test if a function is a coroutine or not, and then treat it appropriately (e.g. by running it in the event loop or in a threadpool)The problem is that redis-py's asyncio system does not use async functions, it uses "normal" functions that return coroutines. This effectively breaks everyone else's ability to treat asyncio redis methods appropriately.
Simple repro
This is a fairly common pattern in other libs. Is there anything that can be done about this so that async functions are properly marked as such?
The text was updated successfully, but these errors were encountered: