Created
April 18, 2014 21:19
-
-
Save csytan/11064907 to your computer and use it in GitHub Desktop.
Tornado async handler with threading
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import time | |
# For python 2.7 use: | |
# pip install futures | |
import concurrent.futures | |
import tornado.ioloop | |
import tornado.web | |
import tornado.gen | |
thread_pool = concurrent.futures.ThreadPoolExecutor(max_workers=4) | |
def future_thread(func): | |
def wrapper(*args, **kwargs): | |
return thread_pool.submit(func, *args, **kwargs) | |
return wrapper | |
@future_thread | |
def blocking(): | |
time.sleep(10) | |
class Index(tornado.web.RequestHandler): | |
@tornado.gen.coroutine | |
def get(self, block=False): | |
if block: | |
yield blocking() | |
self.write('hello world') | |
app = tornado.web.Application([ | |
(r'/', Index), | |
(r'/(block)', Index) | |
]) | |
app.listen(8888) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment