我用以下views.py
创建了一个django api。import datetime
import os
import traceback
from logging.handlers import TimedRotatingFileHandler
from logging import Formatter
from django.http import HttpResponse
import logging
# from . import models as m
import pandas as pd
import json
import psycopg2 as pg
# logger setup
def resp(status):
//returns status code
def index(request):
logger.info('request:' + str(request.GET))
obj = DAA()
if request.method == "GET":
// get values from url request
if (condition):
try:
connection = pg.connect(dbname="dbname", user="user", password="password")
cursor = connection.cursor()
response = obj.level1(arguments)
response = obj.level2(arguments)
try:
// check response
except Exception as e:
# add exception to log
except Exception as e:
# add exception to log
if response :
json_data = {
"responseCode": 200,
"version": "1.0.0",
"results":
[
// response
]
}
json_dump = json.dumps(json_data)
return HttpResponse(json_dump, content_type='application/json')
elif not response :
json_dump = resp(400)
return HttpResponse(json_dump, content_type='application/json')
class DAA:
def level1(arguments):
GB = []
if condition:
query = //query
try:
query_df = pd.read_sql_query(query, connection)
response = [i for i in query_df[//xyz]] // get values
except Exception as e:
cursor.execute('rollback;')
# add exception to log
return None
gb_data = //response
GB.append(gb_data)
return GB
else :
argument = argument .upper()
argument = argument .split(',')
for i, val in enumerate(argument):
query = //query
try:
query_df = pd.read_sql_query(query, connection)
response = [i for i in query_df['xyz']]
except Exception as e:
# add exception to log
return None
gb_data = //response
GB.append(gb_data)
return GB
def level2(arguments):
if condition:
argument = []
try:
cursor.execute( // query )
return response
except Exception as e:
cursor.execute('rollback;')
# add exception to log
return None
else :
argument = []
argument = argument.upper()
argument = argument.split(',')
for i, val in enumerate(argument):
try:
cursor.execute( //query )
except Exception as e:
# add exception to log
return None
return response
使用上述代码,我面临以下问题:
答案 0 :(得分:1)
您是在使用连接后关闭连接还是每次打开一个新连接?
可以尝试使用连接池。创建一个名为mydb.py
的文件import psycopg2
import psycopg2.pool
from contextlib import contextmanager
dbpool = psycopg2.pool.ThreadedConnectionPool(host=<<YourHost>>,
port=<<YourPort>>,
dbname=<<YourDB>>,
user=<<YourUser>>,
password=<<yourpassword>>,
)
@contextmanager
def db_cursor():
conn = dbpool.getconn()
try:
with conn.cursor() as cur:
yield cur
conn.commit()
except:
conn.rollback()
raise
finally:
dbpool.putconn(conn)
然后您的代码可以使用:
import mydb
def myfunction():
with mydb.db_cursor() as cur:
cur.execute("""Select * from blahblahblah...""")