Python 瓶子和Mysql阻塞请求

Python 瓶子和Mysql阻塞请求,python,mysql-python,bottle,Python,Mysql Python,Bottle,我试图弄明白为什么我的简单代码有一个阻塞并发请求: # -*- coding: utf-8 -*- import MySQLdb from bottle import route, run from gevent import monkey monkey.patch_all() cnx = MySQLdb.connect(host='127.0.0.1', port=3306, db='db', user='user', passwd='pass', use_unicode=True, ch

我试图弄明白为什么我的简单代码有一个阻塞并发请求:

# -*- coding: utf-8 -*-
import MySQLdb
from bottle import route, run
from gevent import monkey
monkey.patch_all()


cnx = MySQLdb.connect(host='127.0.0.1', port=3306, db='db', user='user', passwd='pass', use_unicode=True, charset="utf8mb4")
cursor = cnx.cursor()


@route('/testlong', method='GET')
def test_long():
    cursor.execute('SELECT SLEEP(5);')
    return 'finished'


@route('/testfast', method='GET')
def test_fast():
    return 'finished'


if __name__ == '__main__':
    run(host='127.0.0.1', port=46677, debug=True, server='gevent')
如果我跑http://localhost:46677/testlong 同时http://localhost:46677/testfast 在另一个浏览器上,我的第二个请求必须等待第一个请求完成(因此需要5秒)


请问我做错了什么?感谢您的帮助。

MySQLdb包只是C扩展之上的一个很薄的Python包装,这意味着
gevent
无法对其进行修补


您应该使用纯Python MySQL客户端(如),或者使用其他支持线程的框架(如FastAPI/Starlette)。

这似乎意味着您应该
yield
ing,而不是
return
ing。@Snakeharmerb谢谢,我试过了,但现在不行了,您不需要
编码
行。@eatmeimadanish这是我完成的工作,我为MySQLDB修改了此包()。谢谢您的帮助。很高兴提供帮助!如果这回答了您的问题,请单击复选标记将其作为答案。