问题
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