简体   繁体   中英

flask sqlalchemy keeps Postgres transaction idle

Im writting a flask api using flaskrestful,sqlalchemy, Postgres, nginx,uwsgi. Im a newbie to python.These are my configuration

database.py

from cuewords import app
from flask.ext.sqlalchemy import SQLAlchemy
from sqlalchemy.pool import NullPool
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import create_engine
from sqlalchemy import Column, Integer, String , Text , Boolean , DateTime, MetaData, Table ,Sequence
from sqlalchemy.dialects.postgresql import JSON


 Base = declarative_base()
 db_name="postgresql+psycopg2://admin:password@localhost:5434/database_name"
 from sqlalchemy.orm import sessionmaker
 engine = create_engine(db_name,poolclass=NullPool ,echo=True)
 Session = sessionmaker(autocommit=False ,bind=engine)
 connection = engine.connect()
 metadata = MetaData()

api.py

   class Webcontent(Resource):

    def post(self):
    session=Session()
    ...assign some params...
    try:
      insert_data=Websitecontent(site_url,publisher_id)
      session.add(insert_data)
      session.commit()
      Websitecontent.update_url(insert_data.id,session)
    except:
      session.rollback()
      raise
    finally:
      return "Data created "
      session.close()                
  else:  
    return "some value"

Here im first saving the just the url then saving all the content of the site using boilerpipe later .Idea is to move to queue later

model.py

 class Websitecontent(Base):

  @classmethod
  def update_url(self,id,session):
  existing_record=session.query(Websitecontent).filter_by(id=int(id)).first()
  data=Processing.processingContent(str(existing_record.url))

  #boilerpipe processing the content here
  #assigning some data to existing record in session 

  session.add(existing_record)
  session.commit()
  Websitecontent.processingWords(existing_record,session)

  @classmethod
  def processingWords(self,record,session)
    ...processing
    Websitecontent.saveKeywordMapping(session,keyword_url,record)

  @classmethod
  def saveKeywordMapping(session,keyword_url,record)
   session.commit()
   session.close()

So this code works perfectly in locally but its doesnt work in production .So when i check pag_stat_activity it show the state "idle in transaction". The app hangs then i have to restart the servers. i dont get it why session.close() does not close the pool connection why its keeping psql transaction state busy . Guys any help would be really appreciated.

You are returning before closing the session:

  return "Data created "
  session.close()

I think returning inside finally might swallow the exception, as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM