sharing persisten cache between modules

B

Bart Ogryczak

Hi,
I´ve got a problem creating persistent cache, that would be shared
between modules. There a supermodule, which calls submodules. I´d like
submodules to use cache created in the supermodule. The only way I see
right now, is to pass it as function argument, but that would require a
change in API, which I´d prefer to avoid. Is this the only way?
 
F

Fredrik Lundh

Bart said:
I´ve got a problem creating persistent cache, that would be shared
between modules. There a supermodule, which calls submodules. I´d like
submodules to use cache created in the supermodule. The only way I see
right now, is to pass it as function argument, but that would require a
change in API, which I´d prefer to avoid. Is this the only way?

does "module" mean Python module? why not just put the cache management
code in a module that's imported by any submodule that wants to use it ?

</F>
 
B

Bart Ogryczak

Fredrik said:
does "module" mean Python module?

Yes it does.
why not just put the cache management
code in a module that's imported by any submodule that wants to use it ?

The problem is, that then it is not shared. If I do it like that, each
module has it´s own copy of the cache. Maybe I´m doing something
wrong. I´ve made a cache module, imported it in each of the
submodules. I don´t know how to make the data "static".
 
S

Steve Holden

Bart said:
Yes it does.




The problem is, that then it is not shared. If I do it like that, each
module has it´s own copy of the cache. Maybe I´m doing something
wrong. I´ve made a cache module, imported it in each of the
submodules. I don´t know how to make the data "static".
No, it doesn't. At least, not if the cache is global to the module
that's imported by all the other ones.

regards
Steve
 
F

Fredrik Lundh

Bart said:
The problem is, that then it is not shared. If I do it like that, each
module has it´s own copy of the cache.

nope. modules are shared, and all module-level objects are shared as
well. the code in a module is only executed when you import it the
first time; all further imports will get a reference to the same module
instance.
Maybe I´m doing something wrong. I´ve made a cache module, imported it in
> each of the submodules.

that should work. how do you create the cache in the module, and how do
the individual modules access the cache?

</F>
 
D

Dennis Lee Bieber

The problem is, that then it is not shared. If I do it like that, each
module has it´s own copy of the cache. Maybe I´m doing something
wrong. I´ve made a cache module, imported it in each of the
submodules. I don´t know how to make the data "static".

Let me guess... You are using

from cache_module import *

to save having to qualify names with "cache_module."

f-i-* creates local names initially bound to the objects inside a
module, but any "assignment" to such a name later results in the name
being rebound to the new object -- disconnecting from the original.

import cache_module

cache_module.some_name = some_object

maintains the linkage that the name is inside the module, so all other
references that use cache_module.some_name will see the same object.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 
B

Bart Ogryczak

Dennis said:
f-i-* creates local names initially bound to the objects inside a
module, but any "assignment" to such a name later results in the name
being rebound to the new object -- disconnecting from the original.

Great! That was it. Thank you! :)
 
S

Steve Holden

Dennis said:
Let me guess... You are using

from cache_module import *

to save having to qualify names with "cache_module."

f-i-* creates local names initially bound to the objects inside a
module, but any "assignment" to such a name later results in the name
being rebound to the new object -- disconnecting from the original.

import cache_module

cache_module.some_name = some_object

maintains the linkage that the name is inside the module, so all other
references that use cache_module.some_name will see the same object.

Note that you can also do

import cache_module_with_long_name as cm

and then qualify cm to get at the module's attributes, should you have
chosen to call your module something inappropriately long. This reduces
the typing still further.

always-looking-to-avoid-typos-ly y'rs - steve
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top