From: Dave Angel on 12 Oct 2009 09:44 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.
From: VYAS ASHISH M-NTB837 on 12 Oct 2009 12:34 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(a)ieee.org] Sent: Monday, October 12, 2009 7:14 PM To: VYAS ASHISH M-NTB837 Cc: python-list(a)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.
From: Mick Krippendorf on 12 Oct 2009 13:22 VYAS ASHISH M-NTB837 schrieb: > 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? Indeed, there is: def threaded(): """ do threaded stuff here """ Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Now threaded() runs five times. Python is not Java where one has to subclass from Thread (AFAIR the dark ages where I used to speak Javanese). Mick,
From: VYAS ASHISH M-NTB837 on 12 Oct 2009 13:52 The function that I want to run is part of a class, not a standalone function. There are several class member variables also. Regards, Ashish Vyas -----Original Message----- From: python-list-bounces+ntb837=motorola.com(a)python.org [mailto:python-list-bounces+ntb837=motorola.com(a)python.org] On Behalf Of Mick Krippendorf Sent: Monday, October 12, 2009 10:52 PM To: python-list(a)python.org Subject: Re: deepcopy of class inherited from Thread VYAS ASHISH M-NTB837 schrieb: > 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? Indeed, there is: def threaded(): """ do threaded stuff here """ Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Thread(target=threaded).start() Now threaded() runs five times. Python is not Java where one has to subclass from Thread (AFAIR the dark ages where I used to speak Javanese). Mick, -- http://mail.python.org/mailman/listinfo/python-list
From: Mick Krippendorf on 12 Oct 2009 14:07 VYAS ASHISH M-NTB837 schrieb: > > The function that I want to run is part of a class, not a standalone > function. There are several class member variables also. Then try: class MyClass(object): ... def run(self): """ do threaded stuff here """ ... Thread(target=MyClass().run).start() Thread(target=MyClass().run).start() Thread(target=MyClass().run).start() Thread(target=MyClass().run).start() Thread(target=MyClass().run).start() And *please* don't always quote the whole article in your answer. Mick.
|
Next
|
Last
Pages: 1 2 Prev: Python vs Java -> Perché i pythonisti ce l'hanno tanto con Java? Next: Where to find pexpect |