I have a plugin (Last Modified Plugin) that updates date columns in response to to database events. One problem I am facing is that when mulitple database events are happening in succession (e.g. adding multiple books), I sometimes get a variety of apsw.ThreadingViolationError, as illustrated below:
I tried multiple solutions for this (e.g. using QTimer.singleShot, FunctionDispatcher and a combination of both), but the only solution that seems to be working for me is by using a cache.write_lock as illustrated in the code below:
Since this is the first time I am using this, I just want to know if this is the right way to go about things? Are there any pitfalls or gotchas I should be aware of?
Spoiler:
I tried multiple solutions for this (e.g. using QTimer.singleShot, FunctionDispatcher and a combination of both), but the only solution that seems to be working for me is by using a cache.write_lock as illustrated in the code below:
Code:
def update_date_column(self, cache, col, timestamp, book_ids):
if DEBUG:
prints('Last Modified Plugin: Update column ({}) for book_ids: {}'.format(col, book_ids))
id_map = {book_id: timestamp for book_id in book_ids}
f = cache.fields[col]
with cache.write_lock:
f.writer.set_books(id_map, cache.backend)






