deepcopy of class inherited from Thread

VYAS ASHISH M-NTB837 ashish.vyas at motorola.com
Mon Oct 12 12:34:19 EDT 2009


Hi

I have an object which has a run() method. But I can call it only once.
Calling the start() again will give 

RuntimeError: thread already started

So what is the way to do this?

I thought of doing a deep copy of the object, as shallow copy will also
lead to the above error. 
I tried this:
- deepcopy the object
- call start() for the object you got from deepcopy
- delete the object.

Is there a simpler way to achieve this?


Regards,
Ashish Vyas
-----Original Message-----
From: Dave Angel [mailto:davea at ieee.org] 
Sent: Monday, October 12, 2009 7:14 PM
To: VYAS ASHISH M-NTB837
Cc: python-list at python.org
Subject: Re: deepcopy of class inherited from Thread

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