Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

tornado equivalent of delay

Tags:

python

tornado

Is there an equivalent command in tornado for delay function without affecting the main process to sleep (thus the callbacks would execute even when the main thread is dealying a new function call)

like image 917
Gaurav Avatar asked Jun 20 '12 22:06

Gaurav


2 Answers

Try this:

import time
from tornado.ioloop import IOLoop
from tornado.web import RequestHandler, asynchronous
from tornado import gen

class MyHandler(RequestHandler):
    @asynchronous
    @gen.engine
    def get(self):
        self.write("sleeping .... ")
        self.flush()
        # Do nothing for 5 sec
        yield gen.Task(IOLoop.instance().add_timeout, time.time() + 5)
        self.write("I'm awake!")
        self.finish()

Taken from here.

like image 61
Nikolay Fominyh Avatar answered Sep 30 '22 03:09

Nikolay Fominyh


Note that since 4.1 they've added a gen.sleep(delay) method.

so

yield gen.Task(IOLoop.instance().add_timeout, time.time() + 5)

would just become

yield gen.sleep(5)
like image 32
Brent Miller Avatar answered Sep 30 '22 02:09

Brent Miller