Created
          March 24, 2015 01:16 
        
      - 
      
- 
        Save alejandrobernardis/80518a70a40e3c388f08 to your computer and use it in GitHub Desktop. 
    Pepo!
  
        
  
    
      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 | |
| from tornado.concurrent import run_on_executor | |
| from concurrent.futures import ThreadPoolExecutor # `pip install futures` for python2 | |
| MAX_WORKERS = 4 | |
| class Handler(tornado.web.RequestHandler): | |
| executor = ThreadPoolExecutor(max_workers=MAX_WORKERS) | |
| @run_on_executor | |
| def background_task(self, i): | |
| """ This will be executed in `executor` pool. """ | |
| time.sleep(10) | |
| return i | |
| @tornado.gen.coroutine | |
| def get(self, idx): | |
| """ Request that asynchronously calls background task. """ | |
| res = yield self.background_task(idx) | |
| self.write(res) | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment