0

I'm writing a tornado web server and I'm trying to keep it from blocking on one function.

class TokenHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def post(self):
        global t
        email = self.get_argument("text")
        thread = MetaToken.ExeThread(email,t,self._on_response)
        thread.start()
        #data = t.analyze(email)


    def _on_response(self,json):
        self.write(json)
        self.finish()

Analyze is called in t and can take seconds to complete. I'm ok with that as long as other clients requests can be handled at the same time. This works for the most part but will throw an error on some connections that the stream is closed.

carboncomputed
  • 1,630
  • 3
  • 20
  • 42
  • It's difficult to debug your program without knowing how `Metatoken.ExeThread` works. What is that global `t` variable for? Could it be that subsequent requests, using the same global variable, mess up with callbacks, resulting in "stream closed" errors? – lbolla Jun 27 '12 at 08:35

1 Answers1

0

Do not use threads with tornado this way!

If you have heavy task that requires thread - use celery. If task is light - just give it to gen.

Community
  • 1
  • 1
Nikolay Fominyh
  • 8,946
  • 8
  • 66
  • 102