12-15-2014, 02:46 PM | #1 | |
The Grand Mouse 高貴的老鼠
Posts: 71,506
Karma: 306214458
Join Date: Jul 2007
Location: Norfolk, England
Device: Kindle Voyage
|
initialize() documentation
The documentation for initialize() says:
Quote:
Is this expected behaviour? If so, when should a plugin run code that only needs to be run once when the plugin is added to calibre |
|
12-15-2014, 09:37 PM | #2 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
In __init__ or at the module level. Or simply use a global variable to check that the initialization has been performed.
And note that, for file type plugins, the plugin is run in a worker process, which means initialization including __init__ will happen once per worker process (and note that there can be multiple worker processes, and new worker processes can be created at any time). So if you are doing some initialization that needs to occur only once, not once per processes, you will need to use some kind of IPC locking mechanism, like a sentinel directory or a lock file or a global mutex. Last edited by kovidgoyal; 12-15-2014 at 09:40 PM. |
12-16-2014, 01:15 PM | #3 |
The Grand Mouse 高貴的老鼠
Posts: 71,506
Karma: 306214458
Join Date: Jul 2007
Location: Norfolk, England
Device: Kindle Voyage
|
Many thanks.
Could you clarify the difference between the __init__() method and the initialize() method? I'm not clear on what to do in each one. [Later] Hmm... since initialize() is also being called every time the plugin is run, is there much point to it at all anymore? When it was only called when the plugin was installed, I can see what the point of it was, but not anymore. Or I have just not understood at all? Last edited by pdurrant; 12-16-2014 at 02:11 PM. |
12-16-2014, 09:40 PM | #4 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
initialize() is not called every time the plugin is run, only every time it is initialized. You are probably confused because you are talking about a file type plugin run when adding books. If you add a single book at a time, a new worker processes is started for every add, which means the plugin is initialized for every add. However, if you do a recursive add, with more books than the number of CPU cores there will be a max number of worker processes equal to the number of cores on your system (say n) and initialize() will be caled only n times which is less than the number of books being added.
|
12-17-2014, 03:24 AM | #5 |
The Grand Mouse 高貴的老鼠
Posts: 71,506
Karma: 306214458
Join Date: Jul 2007
Location: Norfolk, England
Device: Kindle Voyage
|
Ah. OK. I understand now. Perhaps the documentation of the initialize() should be updated.
|
12-17-2014, 04:53 AM | #6 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
How would the documentation be improved? I'll be happy to add something, but at least to me, the documentation seems pretty correct. It explicitly states that initialize can be called more than once.
|
12-17-2014, 07:15 AM | #7 |
The Grand Mouse 高貴的老鼠
Posts: 71,506
Karma: 306214458
Join Date: Jul 2007
Location: Norfolk, England
Device: Kindle Voyage
|
It reads to me like it should only be called when plugins are added. That's certainly more than once, but a lot less than every time it's instantiated.
How about "Called once when the plugin is initialized. Plugins are initialized every time a new plugin is added, and every time the plugin is started in a new thread." |
12-17-2014, 09:28 PM | #8 |
creator of calibre
Posts: 43,858
Karma: 22666666
Join Date: Oct 2006
Location: Mumbai, India
Device: Various
|
Added.
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
New documentation is up | Jessica Lares | Kindle Fire | 0 | 09-12-2012 10:19 AM |
Initialize languages field | chomse | Library Management | 1 | 11-30-2011 09:22 PM |
Documentation | Shikhin | Kindle Developer's Corner | 2 | 08-07-2011 10:04 PM |
Server Documentation? | Joe McPlumber | Calibre | 4 | 07-05-2010 11:34 PM |
Docs-to-Go Documentation | borisb | enTourage Archive | 2 | 04-29-2010 03:50 AM |