'''Replacement for pickle.dump() using ForkingPickler.'''
时间: 2023-04-09 22:04:58 浏览: 138
可以使用以下代码替代pickle.dump():
```
import os
import pickle
from multiprocessing import Process, Pipe
from multiprocessing.reduction import ForkingPickler
def save_object(obj, filename):
parent_conn, child_conn = Pipe()
process = Process(target=_save_object, args=(child_conn, obj))
process.start()
with open(filename, 'wb') as f:
ForkingPickler(f, protocol=pickle.HIGHEST_PROTOCOL).dump(parent_conn.recv())
process.join()
def _save_object(conn, obj):
conn.send(obj)
conn.close()
```
这个函数使用了ForkingPickler来序列化对象,这样就可以在多进程环境下使用pickle.dump()。
阅读全文