Last active
August 29, 2015 14:09
-
-
Save kowalcj0/df5381f24160a053dff4 to your computer and use it in GitHub Desktop.
Example tornado coroutines that asynchronously save and retrieve document from a CouchDB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import couch | |
from tornado import ioloop, gen | |
def _setup(db_name="test"): | |
return couch.AsyncCouch(db_name) | |
@gen.coroutine | |
def save_user(db, user): | |
result = yield db.save_doc(user) | |
raise gen.Return(result) | |
@gen.coroutine | |
def get_user(db, user_id): | |
try: | |
result = yield db.get_doc(user_id) | |
raise gen.Return(result) | |
except couch.NotFound: | |
print('Document not found') | |
@gen.coroutine | |
def main(): | |
user = {"name": "John"} | |
db = _setup() | |
new_user = yield save_user(db, user) | |
found_user = yield get_user(db, new_user["id"]) | |
print user | |
print db | |
print new_user | |
print found_user | |
if __name__ == "__main__": | |
ioloop.IOLoop().run_sync(main) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment