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.
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')
if __name__ == '__main__':
- NOTMUCH = notmuch.Database(
- path=None,
- mode=notmuch.Database.MODE.READ_WRITE)
app.run(host='0.0.0.0')