Appengine Development

I've been quite enjoying writing some code for Google Appengine this week. Just a small project for querying a variety of book retailer websites for information.

I'm really impressed with it so far. Having the ability to simply import and use things like memcache and use it is excellent. It works locally with some kind of cache, and then when I deploy my app it uses a memcache on the server side seamlessly.

I'm also liking that the urlfetch infrastructure built into appengine supports async downloading of webpages, so i can fire off 5+ API queries simultaneously and then collect the results, instead of doing them in sequence.

Here's a random bit of code I threw together that lets me cache the results of doing API queries in memcache so that a user doing reloads won't cause more API requests to occur than are strictly necessary:

from google.appengine.api import urlfetch
from google.appengine.api import memcache

CACHE_TIMEOUT = 3600 # 1 hour

class APIError(Exception):

def cache_result(rpc, url):
    result = rpc.get_result()
    if result.status_code == 200:
        data = result.content
        memcache.add(url, data, CACHE_TIMEOUT)

def apiquery(url):
    data = memcache.get(url)
    if data is not None:
        decoded_data = json.loads(data)
        return lambda:decoded_data

    logging.debug("Cache miss for %r", url)

    rpc = urlfetch.create_rpc()
    rpc.callback = lambda: cache_result(rpc, url)
    urlfetch.make_fetch_call(rpc, url, method=urlfetch.GET)

    def answer():
        result = rpc.get_result()
        if result.status_code == 200:
            data = result.content
            return json.loads(data)
        raise APIError, "Broken!"
    return answer

With the above code I can now do something like:

queries = [apiquery(url) for url in generate_api_queries()]
for query in queries:
    result = query()
    ... # do stuff with result

I don't really like the variable names I've used, but it's an interesting enough bunch of code linking together a few bits and pieces so I thought I'd throw it on my blog.


car hire madrid said...

Nice article! Thank for sharing!

Unknown said...

mobile application development

I do consider all of the ideas you've introduced to your post. They're very convincing and will certainly work.

ameliehaine said...

This is good but it can be best through animated videos, children love to watch instead of reading and animated stories and animated rhymes are the best way to educate students. Check some sample ideas from here

ahmed said...

where we guarantee for each transfer of luggage as quickly as possible, So make sure that we will move your furniture whether movable furniture residential or office furniture or commercial furniture in the least and quickly.شركة نقل عفش
شركة نقل عفش من الرياض الى الاردن
شركة نقل اثاث من الرياض الى الاردن
شركة نقل عفش ببريدة

meldaresearch said...

Finding the best healthcare research paper services and Healthcare Essay Writing Services is not easy unless one is keen to establish a professional healthcare assignment writing service provider & healthcare homework help online.

commercial furniture said...

Nice article! Thank for sharing!commercial outdoor furniture

stanproperty penthouse said...

nice blog and articles. I am realy very happy to visit your blog.
luxury penthouse

Online Assignment Help said...

Thanks for sharing such a nice piece of information to us. This is very knowledgeable for me. I am an assignment expert and I am offering Case study Help to students of Australia and all over the world.

essay help zone said...

Hello everyone!

I am glad to share some knowledgeable and informative articles for those people who are studying and want to increase their knowledge. Here are some topics that help you to improve your knowledge.
Here are some valuable articles below,
How Professional Essay Writing Services Can Help Students
How To Cope-Up With Essay Writing Challenges
How to Get Away from Plagiarism Throughout Essay Writing
Procedures To Select Professional Essay Writers
Top 5 Reasons Of Why Active Reading Is So Necessary For Students
Top 7 Essay Writing Skills That Students Must Have
Why Academic Essay Writing is So Necessary for Students

Read these articles, and increase your knowledge. If you realize, these articles are informative, so you should share these articles with your friends, colleagues, and classmate. Thanks