Python 发送并发请求时出现InterfaceError(0“”)
为了学术目的,我正在使用Flask和PyMySQL构建一个简单的应用程序,并使用JMeter进行负载测试。它已经接收请求并发送响应。但是,当我发送多个请求时,它会在几个请求之后(有时只有一个请求)抛出下面的错误Python 发送并发请求时出现InterfaceError(0“”),python,flask,pymysql,Python,Flask,Pymysql,为了学术目的,我正在使用Flask和PyMySQL构建一个简单的应用程序,并使用JMeter进行负载测试。它已经接收请求并发送响应。但是,当我发送多个请求时,它会在几个请求之后(有时只有一个请求)抛出下面的错误 Traceback (most recent call last): File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_ap
Traceback (most recent call last):
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 2292, in wsgi_app
response = self.full_dispatch_request()
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 1815, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/username/BossFight/python/helloflask.py", line 82, in get_driver
if (authentication_success(token)):
File "/home/username/BossFight/python/helloflask.py", line 21, in authentication_success
cursor.execute("SELECT COUNT(*) AS matches FROM users WHERE id=%s", (id))
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 170, in execute
result = self._query(query)
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/pymysql/cursors.py", line 328, in _query
conn.query(q)
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/pymysql/connections.py", line 515, in query
self._execute_command(COMMAND.COM_QUERY, sql)
File "/home/username/BossFight/python/venv/lib/python3.6/site-packages/pymysql/connections.py", line 745, in _execute_command
raise err.InterfaceError("(0, '')")
pymysql.err.InterfaceError: (0, '')
在那之后,它不再响应HTTP 500以外的任何东西,必须重新启动
以下是来源的相关部分:
from flask import Flask,jsonify,request
import pymysql.cursors
import bcrypt
import jwt
from pprint import pprint
app = Flask(__name__)
connection = pymysql.connect(
host='localhost',
db='omnibus',
user='starleaf1',
password='alleluia',
cursorclass=pymysql.cursors.DictCursor
)
secret = 'keyboard cat'
def authentication_success(token):
id=jwt.decode(token, secret, algorithms=['HS256'])["id"]
cursor=connection.cursor()
cursor.execute("SELECT COUNT(*) AS matches FROM users WHERE id=%s", (id))
return (cursor.fetchone()['matches'] == 1)
# Other routes goes here....
@app.route('/api/driver/<string:driver_id>', methods=['GET'])
def get_driver(driver_id):
cursor = connection.cursor()
token=request.headers['Authorization'][7:]
if (authentication_success(token)):
cursor.execute(query_get_driver_by_id, (driver_id))
driver=cursor.fetchone()
if (not(driver is None)):
return jsonify(message="ok",driver=driver)
else:
return jsonify(message="not found"),404
else:
return jsonify(message="authentication required"),401
请记住,我是Python的初学者。任何帮助都将不胜感激。问题可能是您有全球连接。在web应用程序中,每个请求都应该有自己的db连接。因此,get_驱动程序函数应该创建一个连接,并在finally子句中完成后关闭该连接 比如:
def create_connection():
return pymysql.connect(
host='localhost',
db='omnibus',
user='starleaf1',
password='alleluia',
cursorclass=pymysql.cursors.DictCursor
)
@app.route('/api/driver/<string:driver_id>', methods=['GET'])
def get_driver(driver_id):
connection = create_connection()
try:
cursor = connection.cursor()
token=request.headers['Authorization'][7:]
if (authentication_success(token)):
cursor.execute("SELECT COUNT(*) AS matches FROM users WHERE id=%s", (driver_id))
driver=cursor.fetchone()
if (not(driver is None)):
return jsonify(message="ok",driver=driver)
else:
return jsonify(message="not found"),404
else:
return jsonify(message="authentication required"),401
finally:
connection.close()
您的代码不包含回溯中的任何一行。@klauds。修好了。那是错误的。哎呀。我现在看到了。我想我已经解决了。我最终实现了一些不同的东西。最值得注意的是,在driver=cursor.fetchone之后调用connection.close