Created
July 25, 2013 00:48
-
-
Save Tydus/6075921 to your computer and use it in GitHub Desktop.
This file contains 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
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