From: W. Trevor King Date: Sun, 21 Sep 2014 20:52:27 +0000 (-0700) Subject: nmhive.py: Use per-request database connections. X-Git-Tag: v0.1.0~17 X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=a98096f1;p=nmhive.git nmhive.py: Use per-request database connections. On #notmuch, Austin Clements just said: Opening the database always gives you a consistent snapshot, so you won't see changes unless you close and reopen it. However, opening in read-only mode is quite cheap. so I'm going to drop my global connection in favor of per-request connections. This also allows me to cleanup after aborted atomic transactions (when I implement the POST backend), because notmuch does not currently expose Xapian::WritableDatabase::cancel_transaction. It also allows other readers to see the new data, since notmuch only commits changes when the database connection is closed. Finally, David Bremner pointed out that holding the read/write lock for an extended period of time is just bad form. --- diff --git a/nmhive.py b/nmhive.py index 2acde49..841bccf 100755 --- a/nmhive.py +++ b/nmhive.py @@ -16,16 +16,19 @@ app.config['CORS_HEADERS'] = 'Content-Type' flask_cors.CORS(app) TAG_PREFIX = os.getenv('NMBPREFIX', 'notmuch::') -NOTMUCH = None -_TAGS = {} +NOTMUCH_PATH = None @app.route('/tags', methods=['GET']) def tags(): tags = set() - for t in NOTMUCH.get_all_tags(): - if t.startswith(TAG_PREFIX): - tags.add(t[len(TAG_PREFIX):]) + database = notmuch.Database(path=NOTMUCH_PATH) + try: + for t in database.get_all_tags(): + if t.startswith(TAG_PREFIX): + tags.add(t[len(TAG_PREFIX):]) + finally: + database.close() return flask.Response( response=json.dumps(sorted(tags)), mimetype='application/json') @@ -77,7 +80,4 @@ def gmane_message_id(group, article): if __name__ == '__main__': - NOTMUCH = notmuch.Database( - path=None, - mode=notmuch.Database.MODE.READ_WRITE) app.run(host='0.0.0.0')