flask sqlalchemy保留Postgres事务id

2024-04-24 16:59:07 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在用flaskrestful,sqlalchemy,Postgres,nginx,uwsgi编写一个flaskapi。我是新手Python。这些是我的配置吗

数据库.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文件

^{pr2}$

在这里,我先保存网址,然后再使用样板文件保存网站的所有内容,想法是以后再转到队列

模型.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()

所以这段代码在本地运行得很好,但在生产环境中却不工作,所以当我检查pag_stat_活动时,它显示状态“idle in transaction”。应用程序挂起,然后我必须重新启动服务器。我不明白为什么会话.关闭为什么保持psql事务的连接池()不关闭。任何人都会很感激的。在


Tags: namefrompyimportidurlsqlalchemysession