Last active
August 29, 2015 14:20
-
-
Save cloverstd/9fd590f683c80cdf6255 to your computer and use it in GitHub Desktop.
python-rq with tornado asynchronous
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
import time | |
def test(interval): | |
for i in xrange(int(interval)): | |
print i | |
time.sleep(1) | |
return interval |
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
#!/usr/bin/env python | |
# encoding: utf-8 | |
# http://tornadogists.com/3849257/ | |
import tornado.ioloop | |
import tornado.httpserver | |
import tornado.web | |
import tornado.gen | |
import redis | |
import tornado.options | |
from rq import Queue | |
import jobs | |
r = redis.Redis() | |
@tornado.gen.coroutine | |
def get_result(job): | |
while True: | |
yield tornado.gen.sleep(0.1) | |
if job.result is not None or job.status == 'failed': | |
break | |
raise tornado.gen.Return(job) | |
class IndexHandler(tornado.web.RequestHandler): | |
@tornado.gen.coroutine | |
def get(self, interval=10): | |
q = Queue(connection=r) | |
job = q.enqueue(jobs.test) | |
job = yield get_result(job) | |
self.write(str(job.result)) | |
if __name__ == '__main__': | |
tornado.options.parse_command_line() | |
application = tornado.web.Application(handlers=[ | |
(r"/(\d+)", IndexHandler) | |
], debug=True) | |
http_server = tornado.httpserver.HTTPServer(application) | |
http_server.listen(8881) | |
tornado.ioloop.IOLoop.instance().start() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment