2009年6月19日 星期五

app engine新功能 工作佇列

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 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'))

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