The new Task Queue API on Google App Engine
今天google app enigne推出了1.23版的新sdk (for Pyhton)
多了一个很有用的功能Task Queue
之前其实已经有了类似的离线工作功能cron job
但是cron job比较像是固定设一个机器人 定时去访问某的特定页面来达成某个功能
可是每个页面的反应时间还是固定的 (30秒)
所以如果我们要做一些可能会花很多时间的重复性工作 就可以用这个新的Task Queue功能
例如我们要寄信给所有的使用者
可以这样写
# for each user, add a task to send a custom email message
for u in users:
taskqueue.add(url='/work/sendmail',
params=dict(to=u.email, subject='Hello ' + u.name, body='this is a message!'))
return # finished now, emails will be sent offline when tasks execute
...
# task handler at /work/sendmail, automatically called for each task created above
class MailWorker(webapp.RequestHandler):
def post(self):
mail.send_mail(
'from_me@example.com',
self.request.get('to'),
self.request.get('subject'),
self.request.get('body'))
这样工作就会拆成一个一个的小单位在工作队列里依序完成
不会受到单一页面的响应时间限制 (因为被自动拆成多次访问 一次访问只寄一封信出去)
虽然cron job可以自己透过一些机制达到同样的功能 不过有了Task Queue使用起来方便很多
而且cron job只能事先写在cron.yaml里 而Task Queue是写在普通页面里
可以有更高的可控弹性
Task Queue目前还在预览版本 工作队列的数量上限等限制可能还会随时调整
原文: http://googleappengine.blogspot.com/2009/06/new-task-queue-api-on-google-app-engine.html
The new Task Queue API on Google App Engine
With release 1.2.3 of the Python SDK, we are psyched to present an exciting new feature - the Task Queue API. You can now perform offline processing on App Engine by scheduling bundles of work (tasks) for automatic execution in the background. You don't need to worry about managing threads or polling - just write the task processing code, queue up some input data, and App Engine handles the rest. If desired, you can even organize and control task execution by defining custom queues. A quick example:
# for each user, add a task to send a custom email messagefor u in users:
taskqueue.add(url='/work/sendmail',
params=dict(to=u.email, subject='Hello ' + u.name, body='this is a message!'))
return # finished now, emails will be sent offline when tasks execute
...
# task handler at /work/sendmail, automatically called for each task created above
class MailWorker(webapp.RequestHandler):
def post(self):
mail.send_mail(
'from_me@example.com',
self.request.get('to'),
self.request.get('subject'),
self.request.get('body'))
We're eager to help you learn and experiment with Task Queues. The team recently presented the feature at Google I/O and the video is now available (slides are here). We've also prepared a set of demos to help you get started. And of course, don't miss the feature documentation. The Task Queue API is Python-only for now; we'll have a Java language version available soon.
Please note that the Task Queue API is currently a Labs release - we want to get your feedback on its usability and functionality before finalizing the API. You'll notice that its Python import path currently includes the 'labs' module (google.appengine.api.labs.taskqueue). Before the feature is promoted out of Labs, we may need to:
- Change the quotas and limits which apply to Task execution (definitely, we hope to raise the number of Tasks you can use per day).
- Change the API itself if there are usability or functionality issues.
- Change how we bill for Task Queue usage.
Once we're ready to promote the feature out of Labs, we'll give weeks of notice and provide a transition path for our developers.
Last but not least, the 1.2.3 release is full of other new stuff as well! Stay tuned to the blog for more updates or check the release notes for exciting info on:
- Asynchronous urlfetch support
- Django 1.0 support
Visit the Downloads page to get SDK 1.2.3 now!
The Task Queue API is the first milestone of our plan to deliver rich support for offline processing. There's more to come, but we hope the simplicity and power of this first release opens a new range of possibilities for our developers. Try it out and let us know! We'll be watching the Group for your input.
-- The App Engine Team相關文章:
Google App Engine Blog: Seriously this time, the new language on App Engine: Java™
Google App Engine Blog: The new Task Queue API on Google App Engine
http://code.google.com/intl/en/appengine/docs/python/config/cron.html
http://code.google.com/intl/en/appengine/docs/python/config/queue.html
...