【问题标题】:flask postgresql connection pool using uwsgi to run behind nginx使用uwsgi在nginx后面运行flask postgresql连接池
【发布时间】:2015-04-30 12:03:28
【问题描述】:

我对 Web 服务器和 Web 应用程序比较陌生。

我们在flask上实现了一个基本的python应用程序,它使用uwsgi部署在服务器上,在nginx服务器后面运行。目前,我们的应用程序必须为每个请求创建一个 postgreSQL 数据库连接,这需要超过理想的时间。我需要的是一个持久连接池,它只创建一次并在每个请求中重复使用。

我尝试使用 pssycopg2 内置功能创建连接池

# create pool with min number of connections of 1, max of 10
a = psycopg2.pool.SimpleConnectionPool(1,10,database='YOURDB', otherstuff...)

但在处理请求时,无法获取要使用的全局持久连接池,可能是因为 UWSGI。 我用烧瓶 UWSGI 查找了连接池,但找不到足够的信息来创建连接池

问题 1:在上述环境(flask + UWSGI + NGINX)中实现连接池的最佳方式是什么?

问题 2:如果我实现像 SQL-Alchemy 这样的 ORM。在上述情况下是否能够提供有效的连接池,或者 UWSGI 会阻止其提供连接池的功能?

【问题讨论】:

    标签: postgresql nginx flask connection-pooling uwsgi


    【解决方案1】:

    有几个 PostgreSQL 池选项:

    我正在使用 psycopg2.pool.ThreadedConnectionPool 和一个小型 Flask 应用程序,它工作正常。

    connection_pool = pool.ThreadedConnectionPool(MINCONN, MAXCONN, database=DB, user=USER, password=PASSWORD)
    conn = connection_pool.getconn()
    # use conn, then:
    connection_pool.putconn(conn)
    

    由您来处理池已满时发生的情况,因为 getconn() 不会阻塞。

    【讨论】:

      猜你喜欢
      • 2013-04-03
      • 2016-01-19
      • 2023-03-13
      • 1970-01-01
      • 2017-12-05
      • 2019-09-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多