Register Guidelines E-Books Today's Posts Search

Go Back   MobileRead Forums > E-Book Software > Calibre > Development

Notices

Reply
 
Thread Tools Search this Thread
Old 08-27-2021, 12:48 PM   #1
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
apsw.ThreadingViolationError and cache.write_lock

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:

Spoiler:

Traceback (most recent call last):
File "threading.py", line 932, in _bootstrap_inner
File "/home/user/calibre/src/calibre/db/backup.py", line 54, in run
self.do_one()
File "/home/user/calibre/src/calibre/db/backup.py", line 125, in do_one
self.db.clear_dirtied(book_id, sequence)
File "/home/user/calibre/src/calibre/db/cache.py", line 77, in call_func_with_lock
return func(*args, **kwargs)
File "/home/user/calibre/src/calibre/db/cache.py", line 1305, in clear_dirtied
self.backend.mark_book_as_clean(book_id)
File "/home/user/calibre/src/calibre/db/backend.py", line 1808, in mark_book_as_clean
self.execute('DELETE FROM metadata_dirtied WHERE book=?', (book_id,))
File "/home/user/calibre/src/calibre/db/backend.py", line 937, in execute
return self.conn.cursor().execute(sql, bindings)
apsw.ThreadingViolationError: You are trying to use the same object concurrently in two threads or re-entrantly within the same thread which is not allowed.


Exception in thread Thread-6:
Traceback (most recent call last):
File "threading.py", line 932, in _bootstrap_inner
File "calibre/db/backup.py", line 54, in run
File "calibre/db/backup.py", line 125, in do_one
File "calibre/db/cache.py", line 77, in call_func_with_lock
File "calibre/db/cache.py", line 1305, in clear_dirtied
File "calibre/db/backend.py", line 1808, in mark_book_as_clean
File "calibre/db/backend.py", line 937, in execute
apsw.ThreadingViolationError: You are trying to use the same object concurrently


Traceback (most recent call last):
File "calibre/db/backup.py", line 63, in do_one
File "calibre/db/cache.py", line 77, in call_func_with_lock
File "calibre/db/cache.py", line 1196, in check_dirtied_annotations
File "calibre/db/backend.py", line 1986, in dirty_books_with_dirtied_annotations
apsw.ThreadingViolationError: You are trying to use the same object concurrently in two threads or re-entrantly within the same thread which is not allowed.
Traceback (most recent call last):
File "calibre/db/cache.py", line 1290, in get_metadata_for_dump
File "calibre/db/cache.py", line 2457, in all_annotations_for_book
File "calibre/db/backend.py", line 1867, in all_annotations_for_book
File "calibre/db/backend.py", line 937, in execute


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)
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?
capink is offline   Reply With Quote
Old 08-27-2021, 01:02 PM   #2
kovidgoyal
creator of calibre
kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.kovidgoyal ought to be getting tired of karma fortunes by now.
 
kovidgoyal's Avatar
 
Posts: 45,347
Karma: 27182818
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
Just use

cache.set_field()
kovidgoyal is offline   Reply With Quote
Advert
Old 08-27-2021, 01:27 PM   #3
capink
Wizard
capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.capink ought to be getting tired of karma fortunes by now.
 
Posts: 1,196
Karma: 1995558
Join Date: Aug 2015
Device: Kindle
OK. I went for set_books() to avoid triggering events for date fields updated by the plugin. But I can solve this by caching timestamps for updated date fields and dropping any events triggered for the said columns shortly after. Thanks for your reply.
capink is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Calibre cache thiago.eec Calibre 14 03-31-2022 06:57 AM
Viewer Cache 8bulochka8 Viewer 6 02-18-2021 11:17 AM
How to clear cache? odamizu Kindle Developer's Corner 8 04-11-2017 11:15 AM
The py27-apsw fontes31 Devices 0 06-26-2016 01:05 PM
Do recipes use a cache? Starson17 Calibre 2 03-10-2010 10:05 PM


All times are GMT -4. The time now is 09:59 AM.


MobileRead.com is a privately owned, operated and funded community.