Masoud Rahimi
Masoud Rahimi

Reputation: 6041

Pickle can't pickle _thread.lock objects

I'm trying to use pickle to save one of my objects but I face this error when trying to dump it:

TypeError: can't pickle _thread.lock objects

It is not clear to me, because I'm not using any locks inside my code. I tried to reproduce this error:

import threading
from time import sleep
import pickle


class some_class:
    def __init__(self):
        self.a = 1
        thr = threading.Thread(target=self.incr)
        self.lock = threading.Lock()
        thr.start()
    def incr(self):
        while True:
            # with self.lock:
            self.a += 1
            print(self.a)
            sleep(0.5)


if __name__ == "__main__":
    a = some_class()
    val = pickle.dumps(a, pickle.HIGHEST_PROTOCOL)
    print("pickle done!")

pickle_thread.py", line 22, in val = pickle.dumps(a, pickle.HIGHEST_PROTOCOL) TypeError: can't pickle _thread.lock objects

If I define a thread lock inside my object I can't pickle it, right? I think the problem here is using threading.lock but is there any workaround for this? Actually, in my main project, I can't find any locks but I've used lots of modules that I can't trace them. What should I look for? Thanks.

Upvotes: 5

Views: 9122

Answers (1)

Lutz Büch
Lutz Büch

Reputation: 363

You can try to customize the pickling method for this class by excluding unpicklable objects from the dictionary:

def __getstate__(self):
    state = self.__dict__.copy()
    del state['lock']
    return state

When unpickling, you can recreate missing objects manually, e.g.:

def __setstate__(self, state):
    self.__dict__.update(state)
    self.lock = threading.Lock() # ???

I don't know enough about the threading module to predict if this is gonna be sufficient.

Upvotes: 6

Related Questions