from bottle import route, request, run, response, hook, HTTPResponse
from memory_profiler import profile
import psycopg2
from psycopg2.pool import ThreadedConnectionPool,AbstractConnectionPool
from contextlib import contextmanager
dbConnection = "dbname='test-db' user='dbuser' host='localhost' password='dbpass'"
connectionpool = ThreadedConnectionPool(1,1,dsn=dbConnection)
@contextmanager
def getcursor():
con = connectionpool.getconn()
try:
yield con.cursor()
finally:
connectionpool.putconn(con)
count=0
@profile
def process(fileOriginalName):
try:
print("process")
fileObj = None
with getcursor() as cur:
cur.execute('SELECT document_object FROM public."Files" WHERE file_original_name=%s', (fileOriginalName,))
fileObj = cur.fetchone()
finally:
return fileObj
#----------------------------------------------------------------------------
@route("/test/", method="GET")
@profile
def root():
global count
try:
data = None
if count == 0:
data = process("file1")
count=1
return data
else:
data = process("file2")
count=0
return data
finally:
return "<h4>Testing file : " + str(count)
run( server='paste', host='0.0.0.0', port=9000, reloader=True)
我正在尝试从Postgres DB中获取一个二进制对象。加载python代码后,它会占用更多内存,而不会将其释放回OS。请检查代码并帮助我修复它