Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2014
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. def blocking_task(n):
  2. for i in xrange(n):
  3. print i
  4. sleep(1)
  5. return i
  6.  
  7. import threading
  8. import datetime
  9. import time
  10.  
  11. def blocking_task(n):
  12. for i in xrange(n):
  13. print i
  14. time.sleep(1)
  15. return i
  16.  
  17. print "This is the begin of a blocking call. Started at %s"
  18. % datetime.datetime.now()
  19. blocking_task(5)
  20. print "This is the end of a blocking call. Ended at %s"
  21. % datetime.datetime.now()
  22.  
  23. th = threading.Thread(target=blocking_task, args=[5])
  24. print "This is the beginning of a non-blocking call. Started at %s"
  25. % datetime.datetime.now()
  26. th.start()
  27. print "This is the end of a non-blocking call. Ended at %s"
  28. % datetime.datetime.now()
  29.  
  30. This is the begin of a blocking call. Started at 2014-04-07 15:14:41.763855
  31. 0
  32. 1
  33. 2
  34. 3
  35. 4
  36. This is the end of a blocking call. Ended at 2014-04-07 15:14:46.768890
  37. This is the beginning of a non-blocking call. Started at 2014-04-07 15:14:46.769160
  38. 0
  39. This is the end of a non-blocking call. Ended at 2014-04-07 15:14:46.769523
  40. 1
  41. 2
  42. 3
  43. 4
  44.  
  45. #!/usr/bin/python
  46.  
  47. import tornado.web
  48. from tornado.ioloop import IOLoop
  49. from tornado import gen
  50. import time
  51.  
  52. @gen.coroutine
  53. def async_sleep(seconds):
  54. yield gen.Task(IOLoop.instance().add_timeout, time.time() + seconds)
  55.  
  56. class TestHandler(tornado.web.RequestHandler):
  57. @gen.coroutine
  58. def get(self):
  59. for i in xrange(100):
  60. print i
  61. yield async_sleep(1)
  62. self.write(str(i))
  63. self.finish()
  64.  
  65.  
  66. application = tornado.web.Application([
  67. (r"/test", TestHandler),
  68. ])
  69.  
  70. application.listen(9999)
  71. IOLoop.instance().start()
  72.  
  73. add_timeout(deadline, callback) # deadline is the number of seconds to wait, callback is the method to call after deadline.
  74.  
  75. add_timeout(time.time() + seconds, ???)
  76.  
  77. yield gen.Task(loop.add_timeout, time.time() + seconds)
  78.  
  79. loop.add_timeout(time.time() + seconds, callback=gen.Callback(some_unique_key))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement