Common use-cases for pickle in Python

后端 未结 9 1580
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-02 04:56

I\'ve looked at the pickle documentation, but I don\'t understand where pickle is useful.

What are some common use-cases for pickle?

9条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 05:17

    Pickling is absolutely necessary for distributed and parallel computing.

    Say you wanted to do a parallel map-reduce with multiprocessing (or across cluster nodes with pyina), then you need to make sure the function you want to have mapped across the parallel resources will pickle. If it doesn't pickle, you can't send it to the other resources on another process, computer, etc. Also see here for a good example.

    To do this, I use dill, which can serialize almost anything in python. Dill also has some good tools for helping you understand what is causing your pickling to fail when your code fails.

    And, yes, people use picking to save the state of a calculation, or your ipython session, or whatever.

提交回复
热议问题