Last active
November 27, 2016 00:12
-
-
Save Dani4kor/efb35dd16e26736a2124fd9bf4752d47 to your computer and use it in GitHub Desktop.
Tornado - Coroutines Time Delay&Sleep Example
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 | |
import tornado.web | |
from tornado.ioloop import IOLoop | |
from tornado import gen | |
from tornado.options import define, options, parse_command_line | |
define("port", default=8888, help="run on the given port", type=int) | |
@gen.coroutine | |
def async_sleep(seconds): | |
yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds) | |
class MainHandler(tornado.web.RequestHandler): | |
@gen.coroutine | |
def get(self): | |
for i in range(100): | |
print i | |
yield async_sleep(0.5) | |
self.write(str(i)) | |
self.finish() | |
def main(): | |
parse_command_line() | |
app = tornado.web.Application([ | |
(r"/", MainHandler), | |
], | |
cookie_secret="01234", | |
template_path=os.path.join(os.path.dirname(__file__), "templates"), | |
static_path=os.path.join(os.path.dirname(__file__), "static"), | |
xsrf_cookies=True, | |
) | |
app.listen(options.port) | |
tornado.ioloop.IOLoop.current().start() | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment