How to use dill library for object serialization with shelve library

て烟熏妆下的殇ゞ 提交于 2021-02-16 21:24:17

问题


I'm using PyMemoize library to cache coroutine. I decorated the coroutine, but when Python calls it, I get:

TypeError: can't pickle coroutine objects

This happens because PyMemoize internally tries to pickle coroutine and store it inside Redis. For this, it uses shelve.Shelf, which in turn uses pickle. The problem is that, by unknown reason, pickle doesn't support pickling coroutines.

I've tried to pickle coroutines with dill and it worked. How do I tell shelve to use dill as serialization backend?

I've tried to monkey-patch shelve, but it didn't work (I don't know why):

import shelve
from dill import Pickler, Unpickler
shelve.Pickler = Pickler
shelve.Unpickler = Unpickler

回答1:


You can save functions with yields, but not generators. From documentation: "dill cannot yet pickle these standard types: frame, generator, traceback."

This code is worked (dill version 0.3.0):

import shelve
from dill import Pickler, Unpickler
shelve.Pickler = Pickler
shelve.Unpickler = Unpickler
d=shelve.open("shelve.dat")
d['1']=Ellipsis
d.close()

But without dill we get "TypeError: can't pickle ellipsis objects":

import shelve
d=shelve.open("shelve.dat")
d['1']=Ellipsis
d.close()


来源:https://stackoverflow.com/questions/52927236/how-to-use-dill-library-for-object-serialization-with-shelve-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!