title: Free up workers for processing while waiting for long DB queries (uWSGI)?
body: <p>I am maintaining API server for my company which runs a python <strong>flask</strong> app in <strong>uwsgi</strong> on top of <strong>nginx</strong>.</p>
  
  <pre><code>...
  
  @app.route('/getquick', methods=["GET"])
  def GET_GET_IP_DATA():
      sp_final = "CALL sp_quick()"
      cursor.execute(sp_final)
  
  @app.route('/get_massive_log', methods=["POST"])
  def get_massive_log():
      sp_final = "CALL sp_slow()"
      cursor.execute(sp_final)
  ...
  </code></pre>
  
  <p>While the first request <strong>/getquick</strong> gets processed very quickly, <strong>/get_massive_log</strong> can take up to five seconds due to a rather <strong>long and complex mySQL query</strong>. The server can handle few of these queries but starts creating broken pipe errors when called to much.</p>
  
  <p><strong>The problem is, the other /getquick requests get blocked by these long I/O requests.</strong></p>
  
  <p>My manager suggested that I use <strong>gevent</strong> to somehow free up the server to process the other requests while waiting for the mySQL queries, but I am not sure if I am looking in the correct direction.</p>
  
  <p>I am using <strong>pymysql</strong> to run queries, which google seems to suggest to work with <strong>gevent on top of uwsgi</strong>, but I have not been able to produce better results with it.</p>
  
  <p>I have googled for days now, and while I am trying to understand threads, concurrency, asynchronous requests, I don't know where to start digging to find a solution. Is it even possible? Any suggestions or even pointers to where to research would be greatly appreciated.</p>
  
  <p><strong>EDIT :</strong> Perhaps my questions wasn't too clear, so I'll try to restate it: </p>
  
  <p><strong>What's the best way to free up workers for processing other requests while waiting for long database queries with uwsgi?</strong></p>