Skip to content

Instantly share code, notes, and snippets.

@emlynoregan
emlynoregan / simpledistpromise.py
Last active August 29, 2015 14:10
Simple Distributed Promise
def MyLongRunningFunction(resolve):
try:
# we're in a separate task. Do something long running!
# eventually call resolve(result), with whatever result you made
except Exception, ex:
resolve(ex)
def MyFunctionToRunAfterThat(result):
try:
value = result.value
@emlynoregan
emlynoregan / gist:1fdab9096aa08e520190
Created November 21, 2014 02:20
Promises using closures
def DoABigThing(aListOfNumbers, callWhenSucceededF, callWhenFailedF):
def DoTheThing(resolve, initialValue):
theSum = sum(aListOfNumbers) + initialValue
theSum += # also get some stuff from a webservice or datastore or something else slow?
resolve(theSum)
def DoneTheThing(result):
try:
callWhenSucceededF(result.value)
except Exception, ex:
@emlynoregan
emlynoregan / gist:9604b963901bfb63a752
Last active August 29, 2015 14:10
Datastore Mapper with Distributed Promises for AppEngine
# This is a generic map function for the datastore.
# Suitable for passing to the PromiseSpace.when() method.
# outerresolve: this function is called when the mapping is complete, with the full result.
# promiseSpace: a promise space, to be used for making promises.
# dsQuery: an ndb datastore query.
# mapF: an optional map function. Takes an object or key (depending on keyOnly) and returns something.
# reduceF: an optional reduce function. Takes a list of results of mapping and a previous return value
# from reduceF, and returns a new value
# keyOnly: determines whether the queries fetch objects or just keys
# pageSize: size of the pages fetched from the query
class Greeting(ndb.Model):
"""Models an individual Guestbook entry with content and date."""
content = ndb.StringProperty()
date = ndb.DateTimeProperty(auto_now_add=True)
class Greeting(ndb.Model):
...
def _post_put_hook(self, future):
# replace the document for this object in the relevant search index
@classmethod
def _post_delete_hook(cls, key, future):
# delete the document for this object from the relevant search index
class Greeting(ndb.Model):
...
def _post_put_hook(self, future):
# replace the document for this object in the relevant search index
@classmethod
def _post_delete_hook(cls, key, future):
# delete the document for this object from the relevant search index
var fooschema = <some schema>
var footransform = <some transform>
var foo = function(inputmas) {
JsonSchema.validate(inputmas, fooschema)
return JsonTransform.transform(inputmas, footransform)
}
{
"cursor": "8b08006b-963a-6909-c132-cc618cd4b352",
"more": true,
"items": [
{
"name": "Ambassador Kosh",
"type": "Vorlonn",
"resources": {
"website": "http://babylon5.wikia.com/wiki/Kosh",
"quote": "Yes"
{
"characters": [
{
"_type": "section",
"path": "$.items[*]",
"transform": {
"fullname": "#@.name",
"saying": {
"_type": "section",
"path": "@.quote",
{
"characters": [
{
"fullname": "Ambassador Kosh",
"saying": "Yes"
},
{
"fullname": "Worf"
},
{