title: Tornado Web and Threads
body: <p>I am new to Tornado and Python Threads. What I would like to achieve is the following:
  I have a Tornado web server which takes requests from users. I want to store some of the data locally and write it periodically to a data base as bulk-inserts.</p>
  
  <pre><code>import tornado.ioloop
  import tornado.web
  import threading
  
  # Keep userData locally in memory
  UserData = {}
  
  def background(f):
      """
      a threading decorator
      use @background above the function you want to thread
      (run in the background)
      """
      def bg_f(*a, **kw):
          threading.Thread(target=f, args=a, kwargs=kw).start()
      return bg_f
  
  @background
  def PostRecentDataToDBThread(iter = -1):
      i = 0
      while iter == -1 or i &lt; iter: 
          #send data to DB
          UserData = {}
          time.sleep(5*60)
          i = i + 1
  
  class AddHandler(tornado.web.RequestHandler):
      def post(self):
          userID = self.get_argument('ui')
          Data = self.get_argument('data')
  
          UserData[userID] = Data 
  
  
  if __name__ == "__main__":
      tornado.options.parse_command_line()
  
      print("start PostRecentDataToDBThread")
      ### Here we start a thread that periodically sends data to the data base.
      ### The thread is called every 5min. 
      PostRecentDataToDBThread(-1)
  
      print("Started tornado on port: %d" % options.port)
  
      application = tornado.web.Application([
          (r"/", MainHandler),
          (r"/add", AddHandler)
      ])
      application.listen(options.port)
      tornado.ioloop.IOLoop.instance().start()
  </code></pre>
  
  <p>Is this a good way to achieve my goal? I would like to minimize the server blocking time. Or should I rather use gevent or anything else? Can I run into problems by accessing UserData both from Tornado and the thread? Data consistency is not so important here as long as there is no server crash. </p>