-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to run the return variable coroutine before starting the application? #1694
Comments
Is there a reason you want to use global variable instead of putting the redis in the app.state? |
Yes, there is. I need to use the connection pool not only in path operation functions. And when you import main.pyfrom fastapi import FastAPI
from connections import get_redis_pool
from red import get_all
app = FastAPI()
@app.on_event('startup')
async def starup_event():
app.state.redis = await get_redis_pool()
@app.on_event('shutdown')
async def shutdown_event():
app.state.redis.close()
await app.state.redis.wait_closed()
@app.get('/')
async def redis_keys():
return await get_all red.pyfrom main import app
async def get_all():
return await app.state.redis.keys('*') connections.pyimport aioredis
async def get_redis_pool() -> aioredis.Redis:
redis = await aioredis.create_redis_pool("redis://localhost:6379/0?encoding=utf-8")
return redis |
@asviel connections.pyfrom aioredis import create_redis_pool, Redis
async def get_redis_pool() -> Redis:
redis = await create_redis_pool("redis://localhost:6379/0?encoding=utf-8")
return redis main.pyfrom fastapi import FastAPI
from connections import get_redis_pool
from uvicorn import run
app = FastAPI()
async def get_all():
return await app.state.redis.keys('*')
@app.on_event('startup')
async def starup_event():
app.state.redis = await get_redis_pool()
@app.on_event('shutdown')
async def shutdown_event():
app.state.redis.close()
await app.state.redis.wait_closed()
@app.get('/')
async def redis_keys():
return get_all()
if __name__ == '__main__':
run("main:app", port=3000, reload=True) |
@MacMacky Yes, that's how I showed you why I can't use the solution suggested above. |
@asviel you've probably solved this by now, but here is a working solution: connections.pyfrom typing import Optional
from aioredis import Redis, create_redis_pool
class RedisCache:
def __init__(self):
self.redis_cache: Optional[Redis] = None
async def init_cache(self):
self.redis_cache = await create_redis_pool("redis://localhost:6379/0?encoding=utf-8")
async def keys(self, pattern):
return await self.redis_cache.keys(pattern)
async def set(self, key, value):
return await self.redis_cache.set(key, value)
async def get(self, key):
return await self.redis_cache.get(key)
async def close(self):
self.redis_cache.close()
await self.redis_cache.wait_closed()
redis_cache = RedisCache() main.pyfrom fastapi import FastAPI
from uvicorn import run
from connections import redis_cache
app = FastAPI()
async def get_all():
return await redis_cache.keys('*')
@app.on_event('startup')
async def starup_event():
await redis_cache.init_cache()
@app.on_event('shutdown')
async def shutdown_event():
redis_cache.close()
await redis_cache.wait_closed()
@app.get('/')
async def redis_keys():
return await get_all()
if __name__ == '__main__':
run("main:app", port=3000, reload=True) This allows you to import redis_cache in other modules without any problems. |
Thanks for the help here everyone! 👏 🙇 Thanks for reporting back and closing the issue 👍 |
put on app.state wiil appear ImportError... My router is not main.py. |
This code helps me to understand what is going on between redis and Fastapi >>>> Thankyou for sharing this >>>> keep help others ... |
This issue was moved to a discussion.
You can continue the conversation there. Go to discussion →
Hi, experts.
I want to create a global connection pool to Redis when the application starts using aioredis. I cannot use the startup event because I need to create a global variable. I was trying to do something like that:
main.py
connections.py
and got an error
RuntimeError: asyncio.run() cannot be called from a running event loop
.Is there any way I can run a coroutine when starting an application to create a global variable?
The text was updated successfully, but these errors were encountered: