Created
March 15, 2023 15:21
-
-
Save tailtq/0ee435d4fe4e881c6ef0218010b59d98 to your computer and use it in GitHub Desktop.
Monkey-patching to override Pickle Serializer (not recommended + error-prone)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# pip install dill | |
import io | |
import dill as pickle | |
from weakref import WeakSet | |
from multiprocessing import reduction | |
class ForkingPickler(reduction.ForkingPickler): | |
loads = pickle.loads | |
@classmethod | |
def dumps(cls, obj, protocol=None): | |
buf = io.BytesIO() | |
pickle.dump(obj, buf) | |
return buf.getbuffer() | |
reduction.pickle = pickle | |
reduction.ForkingPickler = ForkingPickler | |
reduction.dump = pickle.dump | |
# where the program begins | |
from multiprocessing import Process | |
def execute(param=None): | |
print(param, type(param)) | |
if __name__ == "__main__": | |
test = WeakSet() | |
process = Process(target=execute, args=(test,)) | |
process.start() | |
process.join() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment