Skip to content

Instantly share code, notes, and snippets.

@kowalcj0
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kowalcj0/df5381f24160a053dff4 to your computer and use it in GitHub Desktop.
Save kowalcj0/df5381f24160a053dff4 to your computer and use it in GitHub Desktop.
Example tornado coroutines that asynchronously save and retrieve document from a CouchDB
# -*- 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