我的Flask应用响应超时

时间:2018-07-24 07:04:21

标签: python flask sqlalchemy gunicorn flask-restful

嗨,我正在使用docker + Flask + Gunicorn + SQLAlchemy构建RESTful服务。 但是,有时在执行SQL时会超时。

这是我的引擎:

def get_sybase_us_engine(self):
    from spa_manager import spa_config
    new_sybase_engine = create_engine(spa_config["SYBASE_URL"], connect_args=spa_config["SYBASE_CONN_ARGS"], echo=True, echo_pool=True, pool_size=10)
    return new_sybase_engine

是的,sybase。

这是我使用引擎的方式:

import yaml
from flask import Blueprint, request
from flask_restful import Api, Resource
us_sql_test_blueprint = Blueprint('us_sql_test_blueprint', __name__)
us_sql_test_blueprint_api = Api(us_sql_test_blueprint)


class USSQLTest(Resource):

    def post(self):
        """
        SQL Test API
        ---
        parameters:
          - name: json_string
            in: body
            type: string
            required: false
        tags:
          - SQL Test API
        """
        from app.util.sql_engine import SQLEngine
        from app.util.spa_logger import get_logger
        logger = get_logger()
        # get data from request
        spa_data_str = request.get_data()  
        # convert to dict
        requestbody = yaml.safe_load(spa_data_str)  
        logger.info("SQL TEST request incoming!")
        logger.info("Req: %s" % str(requestbody))
        # here I establish connection to DB
        sql_engine = SQLEngine() 
        sybase_us_engine = sql_engine.get_sybase_us_engine()
        sybase_us_conn = sybase_us_engine.connect()
        logger.info("US connection established!")
        from app.implement.spa_db_sql_test import us_sql_test
        try:
            # query data from DB
            result_dict = us_sql_test(sybase_us_conn)
            logger.info("No hang here, query successfully for all connections")
            logger.info(str(result_dict))
            return result_dict
        except Exception, e:
            logger.exception(e.message)
            return e.message
        finally:
            # finnally close connection
            logger.info("Close us connection")
            sybase_us_conn.close()
            sybase_us_engine.dispose()

us_sql_test_blueprint_api.add_resource(USSQLTest, '/sql-test-us')

但是在我的Gunicorn日志中,有一段时间工作超时(pid 265)。

[2018-07-23 17:24:24 +0000] [265] [DEBUG] POST /spa-automation/public/api/sql-test-us
[2018-07-23 17:24:24 +0000] [259] [DEBUG] POST /spa-automation/public/api/sql-test-ca
[2018-07-23 17:26:40 +0000] [301] [DEBUG] POST /spa-automation/public/api/sync-spa-info
[2018-07-23 17:26:40 +0000] [271] [DEBUG] POST /spa-automation/public/api/cust-refresh
[2018-07-23 17:29:24 +0000] [301] [DEBUG] POST /spa-automation/public/api/sql-test-local-mysql
[2018-07-23 17:29:24 +0000] [283] [DEBUG] POST /spa-automation/public/api/sql-test-mysql
[2018-07-23 17:29:24 +0000] [295] [DEBUG] POST /spa-automation/public/api/sql-test-ca
[2018-07-23 17:29:24 +0000] [289] [DEBUG] POST /spa-automation/public/api/sql-test-us
[2018-07-23 17:31:40 +0000] [289] [DEBUG] POST /spa-automation/public/api/cust-refresh
[2018-07-23 17:31:40 +0000] [283] [DEBUG] POST /spa-automation/public/api/sync-spa-info
[2018-07-23 17:34:24 +0000] [289] [DEBUG] POST /spa-automation/public/api/sql-test-mysql
[2018-07-23 17:34:24 +0000] [271] [DEBUG] POST /spa-automation/public/api/sql-test-us
[2018-07-23 17:34:24 +0000] [259] [DEBUG] POST /spa-automation/public/api/sql-test-ca
[2018-07-23 17:34:24 +0000] [295] [DEBUG] POST /spa-automation/public/api/sql-test-local-mysql
[2018-07-23 17:36:40 +0000] [289] [DEBUG] POST /spa-automation/public/api/sync-spa-info
[2018-07-23 17:36:40 +0000] [259] [DEBUG] POST /spa-automation/public/api/cust-refresh
[2018-07-23 17:39:24 +0000] [289] [DEBUG] POST /spa-automation/public/api/sql-test-us
[2018-07-23 17:39:24 +0000] [283] [DEBUG] POST /spa-automation/public/api/sql-test-ca
[2018-07-23 17:39:24 +0000] [295] [DEBUG] POST /spa-automation/public/api/sql-test-mysql
[2018-07-23 17:39:24 +0000] [277] [DEBUG] POST /spa-automation/public/api/sql-test-local-mysql
[2018-07-23 17:39:25 +0000] [7] [CRITICAL] WORKER TIMEOUT (pid:265)

这是我的项目日志:

[2018-07-23 17:24:25,425] - spa_test_us_view.py [Line:32] - [INFO]-[thread:140514956412672]-[process:265] - US connection established!
[2018-07-23 17:24:25,426] - log.py [Line:109] - [INFO]-[thread:140514956412672]-[process:265] - 
        select top 10 * from part_master

[2018-07-23 17:24:25,426] - log.py [Line:109] - [INFO]-[thread:140514956412672]-[process:265] - ()

此进程:265挂在那里,没有进程265的进一步日志

这是我在dockerfile中启动Gunicorn的方法:

ENTRYPOINT gunicorn -w 8 spa_manager:spa_app -b 0.0.0.0:80 --log-level=debug --timeout 900

顺便说一句,如果我不使用flask和Gunicorn,只需使用Python脚本,就不会发生超时。

出了什么问题?我不知道...

0 个答案:

没有答案