How to avoid computation every time a python module is reloaded

后端 未结 13 902
温柔的废话
温柔的废话 2021-02-06 10:55

I have a python module that makes use of a huge dictionary global variable, currently I put the computation code in the top section, every first time import or reload of the mod

13条回答
  •  死守一世寂寞
    2021-02-06 11:15

    I'm going through this same issue... shelve, databases, etc... are all too slow for this type of problem. You'll need to take the hit once, insert it into an inmemory key/val store like Redis. It will just live there in memory (warning it could use up a good amount of memory so you may want a dedicated box). You'll never have to reload it and you'll just get looking in memory for keys

    r = Redis()
    r.set(key, word)
    
    word = r.get(key)
    

提交回复
热议问题