-
Notifications
You must be signed in to change notification settings - Fork 261
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
MySQL - asyncio.Lock get error #118
Comments
got this error 2. is there any update here? |
Can either of you reduce this to a really simple, reproducible example case? |
from fastapi import FastAPI
from databases import Database
from os import environ as env
database = Database(env.get('DB_URL', 'mysql://root:@127.0.0.1:3306/db'), min_size=5, max_size=10)
app = FastAPI()
@database.transaction()
async def blocked():
return {}
@app.get('/')
async def index():
return await blocked()
@app.on_event('startup')
async def setup_db():
await database.connect() test.py import threading
import requests
[threading.Thread(target=requests.get, args=["http://127.0.0.1:8000"]).start() for _ in range(5)] when I comment |
Any reason the issue got closed? |
i have the same problem, do we have any solution? |
@NingziSlay Can you confirm if this is specific to MySQL? |
sorry about poor English, hope you could understand.. i use fastapi too, here is main.py from fastapi import FastAPI
from apps import apis
from libs.db_tools import session
app = FastAPI()
app.include_router(apis, prefix="/api")
@app.on_event("startup")
async def startup():
await session.connect()
@app.on_event("shutdown")
async def shutdown():
await session.disconnect()
if __name__ == '__main__':
import uvicorn
uvicorn.run(
app,
host='0.0.0.0',
port=8080,
) libs.db_tools.py session = databases.Database("mysql+pymysql://root:[email protected]/beat_dev?charset=utf8&use_unicode=1") i write this from libs.db_tools import session
...
@router.get("/test")
async def test():
query = Track.select()
return await session.fetch_all(query) and use ab -n 4 -c 2 http://127.0.0.1:8000/test will raise exception
|
Is there any progress on this issue? |
I change my code like this, and it's work. class SessionMaker(object):
_session = None
@classmethod
def get_session(cls):
if not cls._session:
cls._session = databases.Database(settings.DB_URL)
return cls._session on main.py @app.on_event("startup")
async def startup():
session = SessionMaker.get_session()
await session.connect()
@app.on_event("shutdown")
async def shutdown():
session = SessionMaker.get_session()
await session.disconnect() |
when use databases and aiomysql I get
Task <Task pending coro=<RequestResponseCycle.run_asgi() running at /usr/local/lib/python3.7/site-packages/uvicorn/protocols/http/httptools_impl.py:370> cb=[set.discard()]> got Future <Future pending> attached to a different loop
detail:
packages:
db.py
execute code snippet
run in docker with base image: python 3.7.3-alpine.
why did this happend? and how to fix it ... it's urgent.
The text was updated successfully, but these errors were encountered: