![]() |
#1 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 183
Karma: 3587000
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 Code:
print("Counter is " + str(getCounter())) incCounter() print("Counter is " + str(getCounter())) 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 ... |
![]() |
![]() |
![]() |
#2 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,176
Karma: 27110894
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".
|
![]() |
![]() |
Advert | |
|
![]() |
#3 |
Groupie
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 183
Karma: 3587000
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)?
|
![]() |
![]() |
![]() |
#4 |
creator of calibre
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Posts: 45,176
Karma: 27110894
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.
|
![]() |
![]() |
![]() |
|
![]() |
||||
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 |