From a98096f129165be003294eaa5ad3596931c58ae7 Mon Sep 17 00:00:00 2001 From: "W. Trevor King" Date: Sun, 21 Sep 2014 13:52:27 -0700 Subject: [PATCH] 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. --- nmhive.py | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) 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') -- 2.26.2