How to avoid computation every time a python module is reloaded

后端 未结 13 712
温柔的废话
温柔的废话 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:13

    1. Factor the computationally intensive part into a separate module. Then at least on reload, you won't have to wait.

    2. Try dumping the data structure using protocol 2. The command to try would be cPickle.dump(FD, protocol=2). From the docstring for cPickle.Pickler:

      Protocol 0 is the
      only protocol that can be written to a file opened in text
      mode and read back successfully.  When using a protocol higher
      than 0, make sure the file is opened in binary mode, both when
      pickling and unpickling. 
      

提交回复
热议问题