我抓住了pyscopg2+flask+uwsgi
的奇怪行为。我试图用apache bench测试一个烧瓶api。对于前2-3个请求,我一直收到此错误:
DatabaseError: SSL error: decryption failed or bad record mac
此后又抛出两个错误:
OperationalError: SSL SYSCALL error: EOF detected
然后请求开始正常工作。 我正在使用这个ab命令:
ab -n 100 -c 10 "my_url"
以下是我与pyscopg2建立联系的方式:
from contextlib import contextmanager
from psycopg2.pool import ThreadedConnectionPool
pool = ThreadedConnectionPool(
1, 20,
dbname=pg_name,
user=pg_username,
host=pg_host,
password=pg_password)
@contextmanager
def get_db_connection():
"""
psycopg2 connection context manager.
Fetch a connection from the connection pool and release it.
"""
try:
connection = pool.getconn()
yield connection
finally:
pool.putconn(connection)
@contextmanager
def get_db_cursor(commit=False):
"""
psycopg2 connection.cursor context manager.
Creates a new cursor and closes it, commiting changes if specified.
"""
with get_db_connection() as connection:
cursor = connection.cursor()
try:
yield cursor
if commit:
connection.commit()
finally:
cursor.close()
with get_db_cursor() as cur:
cur.execute("myquery1")
cur.execute("myquery2")
cur.execute("myquery3")
最后我为uwsgi运行此命令:
uwsgi --http 0.0.0.0:8081 --wsgi-file my_api.py --master --processes 4 --threads 2 --callable app
谁能告诉我我做错了什么?