Coroutine Data Access Patterns (codap) are a handful of libraries to make concurrent data access simpler to use. This was originally developed for web services access data in mutliple datastores (MongoDB, S3, REST, ect). The library uses async methods (thread, eventlet or gevent) which degrades gracefully. Perfering gevent, eventlet and falling back to threading. One of those things it would be really nice to have anonymous functions in Python :(.
pip install codap
Allows for a dictionary like access. This is great for caches, template rendering and since dictionaries are the most used data type it is easy to integrate into existing code.
Example:
defback(db, name):
returndb.find(name)
results=KV()
results['foo'] =bar# bar is a functionresults.put('cats', get_photos, id, limit=4) # get_photos is a functionresults.put('monkey', back, db, name)
render_template('my_temp.html', **results)List that returns the responses based on the order they are added. Has been used for retrieving already sorted data that needs additional information to be rendered.
Example:
defget_stuff(db, x):
returndb.get(x)
results=codap.Ordered()
forxinxrange(0, 10):
results.push(get_stuff, db, x)
forrinresults: # Same order as pushedr.render()Based on the order of the response is the order it is added to the list. Useful for making request to multiple databases. Has been used for getting a list of files from a web service and compressing them into a single zip or tar.
Example:
defget_data(id):
returnmy_data[id]
fr=codap.FirstReply()
fordsindatasource_list:
fr.push(get_data, id)
data=fr[0]importcodapdeffib(n):
ifn==1:
return1elifn==0:
return0else:
returnfib(n-1) +fib(n-2)
FIB_SIZE=30d=codap.KV()
# Push a bunch of fib processing into the backgroundforiinrange(0, FIB_SIZE):
d.put(i, fib, i)
# Pull them out from the listforiinrange(0, FIB_SIZE):
assertd[i] ==fib(i), 'Expected fib {0} to be {1} but was {3}'.format(i, d[i], fib(i))
d=codap.Ordered()
foriinrange(0, FIB_SIZE):
d.push(fib, i)
i=0forfind:
assertf==fib(i), 'Expected fib {0} to be {1} but was {3}'.format(i, f, fib(i))
i+=1