Django PSYCOPG2数据库连接

时间:2019-01-24 05:35:10

标签: python django multithreading apache psycopg2

我用以下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

使用上述代码,我面临以下问题:

  1. 当在jmeter上为100个以上的用户测试应用程序时,我收到一个错误“已经有太多客户端”,似乎是conf文件[max_connections = 100]中的postgres数据库存在问题,但是增加了连接似乎对我没有用,所以请提出其他建议。
  2. 即使运行少于100个用户的应用程序,我也会收到此错误“ RuntimeError:无法启动新线程”,所以请提出如何解决此错误的建议?
  3. 注意:-请忽略编程错误,因为我只共享我的代码的伪结构。

1 个答案:

答案 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...""")