How do I access data programatically (load a pickled file) that is stored as a static file?
how is this solution I am using now:
I have a 1MB .dbf file in the same directory of all my开发者_运维百科 .py modules. In main.py
I have
import tools
In tool.py
code is :
the_list_that_never_changes = loadDbf(file).variables['CNTYIDFP'].
So the_list_that_never_changes
is only loaded once and is always in memory ready to be used...correct?
Static files are stored apart from application files. If you need to load data.pkl
from main.py
, then don't mark it as a static file and it will be accessible by main.py
like any other application file.
Reference: Application Configuration's Handlers For Static Files.
Alternative: Why not define the information stored in data.pkl
as a global variable in your Python source? Then you don't have to go through the trouble of reading a file and deserializing its pickled contents, and it will be a bit faster too. This will also make it easy to take advantage of app caching - your data will be loaded once, and then cached for use by subsequent requests.
Put data.pkl
in the same directory with main.py
and use something along these lines:
pickle_path = os.path.join(os.path.dirname(__file__), 'data.pkl')
f = open(pickle_path)
data = pickle.load(f)
Do not add data.pkl
to app.yaml
.
If you read this data often, it might be beneficial to memcache it after unpickling. Then you can read it from memcache, which is usually faster than reading file from disk.
精彩评论