import tornado.webfrom tornado.concurrent import run_on_executorfrom concurrent.futures import ThreadPoolExecutorimport time
class MainHandler(tornado.web.RequestHandler): def get(self): self.write("Hello, world %s" % time.time())
class SleepHandler(tornado.web.RequestHandler): @property def executor(self): return self.application.executor
@tornado.gen.coroutine def get(self, n): n = yield self._exe(n) self.write("Awake! %s" % time.time()) self.finish()
@run_on_executor def _exe(self, n): """ This is a long time job and may block the server,such as a complex DB query or Http request. """ time.sleep(float(n)) return n
class App(tornado.web.Application): def __init__(self): handlers = [ (r"/", MainHandler), (r"/sleep/(\d+)", SleepHandler), ] tornado.web.Application.__init__(self, handlers) self.executor = ThreadPoolExecutor(max_workers=60)
if __name__ == "__main__": application = App() application.listen(8888) tornado.ioloop.IOLoop.instance().start()Hi, thereI am writing a web API using Tornado(version 4.1) , but I suffered a lot of blocking problems. After searching a lot of tech blogs, I find a method as follow:But I still have some questions need your help:1. The code can realize a asynchronous job, but is it a good way? What happened to tornado when the task is submitted?
2. Tornado is single thread, so this is a new thread pool in this main thread? should the thread pool be global or local for the handle class?
3. If I send a request from Firefox the app seems to be not asynchronous ,but if I send a request from python ,it is asynchronous , so wired! Why?
--
You received this message because you are subscribed to the Google Groups "Tornado Web Server" group.
To unsubscribe from this group and stop receiving emails from it, send an email to python-tornad...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.