Skip to content
Browse files

Adds integration examples

  • Loading branch information...
1 parent b6c8514 commit bfd46b1254455ff8fa0b61c03f17077032c7514d @mher committed May 11, 2012
Showing with 83 additions and 0 deletions.
  1. 0 examples/__init__.py
  2. +64 −0 examples/tornado_async.py
  3. +19 −0 examples/tornado_ioloop.py
View
0 examples/__init__.py
No changes.
View
64 examples/tornado_async.py
@@ -0,0 +1,64 @@
+from urllib import urlencode
+
+from tornado import gen
+from tornado import ioloop
+from tornado.httpclient import AsyncHTTPClient
+from tornado.web import asynchronous, RequestHandler, Application
+
+
+class AsyncHandler(RequestHandler):
+ @asynchronous
+ def get(self):
+ http_client = AsyncHTTPClient()
+ http_client.fetch("http://localhost:8888/apply/tasks.sleep",
+ callback=self.on_task_complete,
+ method="POST",
+ body=urlencode({"args": 3}))
+
+ def on_task_complete(self, response):
+ self.write("Completed!")
+ self.finish()
+
+
+class GenAsyncHandler(RequestHandler):
+ @asynchronous
+ @gen.engine
+ def get(self):
+ http_client = AsyncHTTPClient()
+ response = yield gen.Task(http_client.fetch,
+ "http://localhost:8888/apply/tasks.sleep",
+ method="POST",
+ body=urlencode({"args": 3}))
+ self.write(str(response.body))
+ self.finish()
+
+
+class GenMultipleAsyncHandler(RequestHandler):
+ @asynchronous
+ @gen.engine
+ def get(self):
+ http_client = AsyncHTTPClient()
+ task1 = gen.Task(http_client.fetch,
+ "http://localhost:8888/apply/tasks.sleep",
+ method="POST",
+ body=urlencode({"args": 3}))
+ task2 = gen.Task(http_client.fetch,
+ "http://localhost:8888/apply/tasks.add",
+ method="POST",
+ body=urlencode({"args": [1, 2]}))
+
+ response1, response2 = yield [task1, task2]
+ self.write(str(response1.body))
+ self.finish()
+
+
+application = Application([
+ (r"/async-sleep", AsyncHandler),
+ (r"/gen-async-sleep", GenAsyncHandler),
+ (r"/gen-async-sleep-add", GenMultipleAsyncHandler),
+])
+
+
+if __name__ == "__main__":
+ application.listen(8887)
+ ioloop.IOLoop.instance().start()
View
19 examples/tornado_ioloop.py
@@ -0,0 +1,19 @@
+from urllib import urlencode
+
+from tornado import ioloop
+from tornado import httpclient
+
+
+def handle_request(response):
+ if response.error:
+ print "Error:", response.error
+ else:
+ print response.body
+ ioloop.IOLoop.instance().stop()
+
+
+http_client = httpclient.AsyncHTTPClient()
+http_client.fetch("http://localhost:8888/apply/tasks.sleep",
+ handle_request, method="POST",
+ body=urlencode({"args": 3}))
+ioloop.IOLoop.instance().start()

0 comments on commit bfd46b1

Please sign in to comment.
Something went wrong with that request. Please try again.