import tornado.web
import Queue
QUEUE = Queue.Queue()
class HandlerA( tornado.web.RequestHandler ):
def get(self):
global QUEUE
self.finish(QUEUE.get_nowait())
class HandlerB( tornado.web.RequestHandler ):
def get(self):
global QUEUE
QUEUE.put('Hello')
self.finish('In queue.')
Problem: HandlerA blocks HandlerB for 10 seconds.
- Browser A handled by HandlerA and waits...
- Browser B handled by HandlerB and waits.... till timeout exceptions
Goal
- Browser A handled by HandlerA and waits...
- Browser B handled by HandlerB and returns
- HandlerA returns after dequeuing
Is this an issue with Non-blocking, async, epoll or sockets?
Thanks!
UPDATE:
I updated this code with a new thread to handle the Queue.get_nowait() request. Which I fear is a HORRIBLE solution considering I'm going to have thousands of requests at once and would therefore have thousands of threads at once. I'm considering moving to a epoll
style in the near future.
class HandlerA( tornado.web.RequestHandler ):
@tornado.web.asynchronous
def get(self):
thread.start_new_thread(self.get_next)
def get_next(self):
global QUEUE
self.finish(QUEUE.get_nowait())
Now this is not the best way to handle it... but at least its a start.
SOLUTION
Found here Running blocking code in Tornado