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 12-12-2021, 04:40 AM   #1
Leseratte_10
Groupie
Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.
 
Posts: 181
Karma: 2010542
Join Date: Sep 2021
Device: PB Era, PB InkPad 3 Pro
Keep global variable between different plugin runs

I'm trying to keep a particular setting / variable for the entire duration that Calibre is running (in a FileTypePlugin)

I tried just putting a python variable inside one of the .py files, accessing it with the "global" modifier, but it looks like all the plugins get unloaded for each book.

I have this code in one of my files:

Code:
counter = 0

def getCounter():
    global counter
    return counter

def incCounter(): 
    global counter
    counter = counter + 1
and then I have this code in the initialize() function of my plugin (just to test if it works):

Code:
print("Counter is " + str(getCounter()))
incCounter()
print("Counter is " + str(getCounter()))
When I then import a book into Calibre, it prints "Counter is 0", then "Counter is 1". However, when I import another book after that, it again prints "Counter is 0" instead of continuing with "Counter is 2" which is what I would have expected.

I could store this counter variable inside the JSON settings file like I do with user-defined settings already, but then how do I get the counter reset to 0 when restarting Calibre?

I just want to keep this counter in-between different executions of my plugin, but have it reset to 0 when Calibre completely exits. Is this possible somehow?

Or do I have to use tricks like opening a PersistentTemporaryFile, writing its path to the plugin config (to remember the path between plugin runs), then check if the file is still present or if it's been deleted to detect if Calibre has restarted in the meantime? Sounds like an unreliable workaround ...
Leseratte_10 is offline   Reply With Quote
Old 12-12-2021, 04:46 AM   #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: 43,843
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
file type plugins are run in worker processes so there is no way to store global state in RAM between them. You have to use the filesystem. And they can be run from the commadn line calibredb and calibre-server as well, so there is no concept of "calibre exiting".
kovidgoyal is offline   Reply With Quote
Advert
Old 12-12-2021, 04:53 AM   #3
Leseratte_10
Groupie
Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.Leseratte_10 ought to be getting tired of karma fortunes by now.
 
Posts: 181
Karma: 2010542
Join Date: Sep 2021
Device: PB Era, PB InkPad 3 Pro
Okay, thanks. So when do PersistentTemporaryFiles disappear / get deleted when using calibre-server? I'm assuming for the server it's whenever the actual Calibre server itself restarts (so, probably very rarely, only when the admin manually restarts the calibre server)?
Leseratte_10 is offline   Reply With Quote
Old 12-12-2021, 05:09 AM   #4
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: 43,843
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
It depends. The server might do request based cleanup of temp files. I dont recall if it does, but it might very well do so in the future. If you want to persist things use the cache folder not temp files.
kovidgoyal is offline   Reply With Quote
Reply


Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Environment variable for libraries? Stevex Calibre 21 03-14-2017 12:05 PM
Multiple Windows users: their own plugin settings? Are plugin settings global? oj829 Plugins 2 12-07-2012 12:33 PM
Variable payments come to eBooks WhichP News 4 08-04-2012 03:52 PM
How is it possible to tell difference b/w Global and non Global? steffi Amazon Kindle 3 01-24-2010 11:07 AM


All times are GMT -4. The time now is 10:48 AM.


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