Skip to content

Instantly share code, notes, and snippets.

@Tydus
Created July 25, 2013 00:48
Show Gist options
  • Save Tydus/6075921 to your computer and use it in GitHub Desktop.
Save Tydus/6075921 to your computer and use it in GitHub Desktop.
class QueuedRequest:
def __init__(self):
self.http_client = tornado.web.AsyncHTTPClient()
self.request_queue = Queue()
self.cb = tornado.ioloop.IOLoop.PeriodicCallback(
consume_request,
8 * 60 * 1000,
)
self.cb.start()
@tornado.gen.coroutine
def enqueue_and_yield(self, request):
request_queue.put(request)
response = yield tornado.gen.Wait(request)
raise tornado.gen.Return(response.body)
@tornado.gen.coroutine
def consume_request(self):
if not request_queue.empty():
request = request_queue.get()
self.http_client.fetch(
request,
callback = (yield tornado.gen.callback(request)),
)
# Sample
qr = QueuedRequest()
def go():
request = do_sth()
response_body = enqueue_and_yield()
do_sth_with(response_body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment