Skip to content
Browse files

Examples use Task class

  • Loading branch information...
1 parent 0569374 commit 6e402b1b0915efb2169b74f4d9835a5f029263f8 @mher committed May 19, 2012
Showing with 13 additions and 34 deletions.
  1. +3 −2 examples/tasks.py
  2. +7 −25 examples/tornado_async.py
  3. +3 −7 examples/tornado_ioloop.py
View
5 examples/tasks.py
@@ -1,4 +1,5 @@
import time
+from datetime import datetime
from celery import Celery
@@ -18,8 +19,8 @@ def sleep(seconds):
@celery.task
-def echo(msg):
- return msg
+def echo(msg, timestamp=False):
+ return "%s: %s" % (datetime.now(), msg) if timestamp else msg
@celery.task
View
32 examples/tornado_async.py
@@ -1,19 +1,14 @@
-from urllib import urlencode
-
from tornado import gen
from tornado import ioloop
-from tornado.httpclient import AsyncHTTPClient
from tornado.web import asynchronous, RequestHandler, Application
+from tcelery import Task
+
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}))
+ Task("tasks.sleep", callback=self.on_task_complete)(3)
def on_task_complete(self, response):
self.write("Completed!")
@@ -24,11 +19,7 @@ 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}))
+ response = yield Task("tasks.sleep", 3)
self.write(str(response.body))
self.finish()
@@ -37,18 +28,9 @@ 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))
+ r1, r2 = yield [Task("tasks.sleep", 3), Task("tasks.add", 1, 2)]
+ self.write(str(r1.body))
+ self.write(str(r2.body))
self.finish()
View
10 examples/tornado_ioloop.py
@@ -1,7 +1,6 @@
-from urllib import urlencode
-
from tornado import ioloop
-from tornado import httpclient
+
+from tcelery import Task
def handle_request(response):
@@ -12,8 +11,5 @@ def handle_request(response):
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}))
+Task("tasks.sleep", callback=handle_request)(3)
ioloop.IOLoop.instance().start()

0 comments on commit 6e402b1

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