title: Real-time stat mysql in Python
body: <p>I have about 300+ tables to count in mysql every 10 seconds. Each table correspond each stat table </p>
  
  <p>Example :</p>
  
  <pre><code>def pool_func(pool, table_name):
      for name in table_name:
          pool.add(gevent.spawn(BaseStat(table_name=name).run))
      pool.join()
  </code></pre>
  
  <p>BaseStat.run :(I use crontab to deploy, just run every minute)</p>
  
  <p><em>self.frequency = 10</em></p>
  
  <pre><code>def run(self):
      try:
          if self.table_name is None:
              raise Exception("You should set BaseStat table_name")
          self.stat_table_name = self.table_name + self.table_stat_suffix
  
          start = 0
          self._run_stat()
          while start &lt; 60:
              start += self.frequency
              gevent.sleep(self.frequency)
              self.start_time, self.end_time = self._get_time(self.start_time, self.end_time)
              self._run_stat()
      except Exception as e:
          logger.get("stat-log").error("[%s] : %s" % (self.table_name, util.error_msg()))
  </code></pre>
  
  <p>When the amount of table small it can work well but now it at least 2 minutes delay.Total time running <code>pool_func</code> about 578.056058168 seconds</p>