title: My Flask App response timeout
body: <p>Hi  I'm using docker + Flask + Gunicorn + SQLAlchemy to built a RESTful service.
  However, sometimes it timeout when executing SQL.</p>
  
  <p>Here is my engine:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>That's right , sybase.</p>
  
  <p>Here is how I use engine:</p>
  
  <pre><code>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')
  </code></pre>
  
  <p>However in my Gunicorn log , some time the work timeout(pid 265).</p>
  
  <pre><code>[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)
  </code></pre>
  
  <p>Here is my project log :</p>
  
  <pre><code>[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] - ()
  </code></pre>
  
  <p>This process:265 hangs there and no further log for process:265</p>
  
  <p>Here is how I start my Gunicorn in dockerfile:</p>
  
  <pre><code>ENTRYPOINT gunicorn -w 8 spa_manager:spa_app -b 0.0.0.0:80 --log-level=debug --timeout 900
  </code></pre>
  
  <p>BTW if I don't use flask and Gunicorn , just use Python script , no timeout happen.</p>
  
  <p>What is going wrong? I've no idea...</p>