deepcopy of class inherited from Thread
Dave Angel
davea at ieee.org
Mon Oct 12 09:44:06 EDT 2009
VYAS ASHISH M-NTB837 wrote:
> Dear All
>
> I am running this piece of code:
>
> from threading import Thread
> import copy
>
> class Ashish(Thread):
> def __init__(self, i):
> Thread.__init__(self)
> self.foo = i
> def run(self):
> print (self, self.foo)
>
>
> d= Ashish(4)
> e = copy.deepcopy(d) <--- Exception here....
>
> d.start()
> e.start()
>
> d.join()
> e.join()
>
> But I am getting this error:
>
>
> Traceback (most recent call last):
> File "D:\Profiles\ntb837\Desktop\threadprob.py", line 13, in <module>
> e = copy.deepcopy(d)
> File "C:\Python31\lib\copy.py", line 173, in deepcopy
> y = _reconstruct(x, rv, 1, memo)
> File "C:\Python31\lib\copy.py", line 295, in _reconstruct
> state = deepcopy(state, memo)
> File "C:\Python31\lib\copy.py", line 146, in deepcopy
> y = copier(x, memo)
> File "C:\Python31\lib\copy.py", line 235, in _deepcopy_dict
> y[deepcopy(key, memo)] = deepcopy(value, memo)
> File "C:\Python31\lib\copy.py", line 173, in deepcopy
> y = _reconstruct(x, rv, 1, memo)
> File "C:\Python31\lib\copy.py", line 295, in _reconstruct
> state = deepcopy(state, memo)
> File "C:\Python31\lib\copy.py", line 146, in deepcopy
> y = copier(x, memo)
> File "C:\Python31\lib\copy.py", line 235, in _deepcopy_dict
> y[deepcopy(key, memo)] = deepcopy(value, memo)
> File "C:\Python31\lib\copy.py", line 173, in deepcopy
> y = _reconstruct(x, rv, 1, memo)
> File "C:\Python31\lib\copy.py", line 280, in _reconstruct
> y = callable(*args)
> File "C:\Python31\lib\copyreg.py", line 88, in __newobj__
> return cls.__new__(cls, *args)
> TypeError: object.__new__(_thread.lock) is not safe, use
> _thread.lock.__new__()
>
>
> Could someone please help me find a solution?
>
> Regards,
> Ashish Vyas
>
>
>
Is there some reason you need to copy such an object? In general, you
can get into trouble doing deep copies of structures which involve OS
data, because not all such data can be safely copied. Sometimes such
copies just quietly malfunction, but this time you were fortunate enough
to get a runtime error.
What is your use-case? Perhaps there's some other approach that would
accomplish the real task.
More information about the Python-list
mailing list