Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def blocking_task(n):
- for i in xrange(n):
- print i
- sleep(1)
- return i
- import threading
- import datetime
- import time
- def blocking_task(n):
- for i in xrange(n):
- print i
- time.sleep(1)
- return i
- print "This is the begin of a blocking call. Started at %s"
- % datetime.datetime.now()
- blocking_task(5)
- print "This is the end of a blocking call. Ended at %s"
- % datetime.datetime.now()
- th = threading.Thread(target=blocking_task, args=[5])
- print "This is the beginning of a non-blocking call. Started at %s"
- % datetime.datetime.now()
- th.start()
- print "This is the end of a non-blocking call. Ended at %s"
- % datetime.datetime.now()
- This is the begin of a blocking call. Started at 2014-04-07 15:14:41.763855
- 0
- 1
- 2
- 3
- 4
- This is the end of a blocking call. Ended at 2014-04-07 15:14:46.768890
- This is the beginning of a non-blocking call. Started at 2014-04-07 15:14:46.769160
- 0
- This is the end of a non-blocking call. Ended at 2014-04-07 15:14:46.769523
- 1
- 2
- 3
- 4
- #!/usr/bin/python
- import tornado.web
- from tornado.ioloop import IOLoop
- from tornado import gen
- import time
- @gen.coroutine
- def async_sleep(seconds):
- yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds)
- class TestHandler(tornado.web.RequestHandler):
- @gen.coroutine
- def get(self):
- for i in xrange(100):
- print i
- yield async_sleep(1)
- self.write(str(i))
- self.finish()
- application = tornado.web.Application([
- (r"/test", TestHandler),
- ])
- application.listen(9999)
- IOLoop.instance().start()
- add_timeout(deadline, callback) # deadline is the number of seconds to wait, callback is the method to call after deadline.
- add_timeout(time.time() + seconds, ???)
- yield gen.Task(loop.add_timeout, time.time() + seconds)
- loop.add_timeout(time.time() + seconds, callback=gen.Callback(some_unique_key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement