Python restart

I am using threading.py and I have the following code:

import threading class MyClass(threading.Thread): def __init__(self,par1,par2): threading.Thread.__init__(self) self.var1 = par1 self.var2 = par2 def run(self): #do stuff with var1 and var2 while conditions are met ... ... ... myClassVar = MyClass("something",0.0) 

And I get the following error:

 18:48:08 57 SE myClassVar = MyClass("something",0.0) 18:48:08 58 SE File "C:\Python24\Lib\threading.py", line 378, in `__init__` 18:48:08 59 SE assert group is None, "group argument must be None for now" 18:48:08 60 SE AssertionError: group argument must be None for now 

I kinda like new using python, this is the first time I use threading ...

What is the mistake here?

Thanks,

Jonathan

+4
source share
2 answers

You do not need to extend Thread to use threads. I usually use this template ...

 def worker(par1, par2): pass # do something thread = threading.Thread(target=worker, args=("something", 0.0)) thread.start() 
+7
source

You can also use the class and override the stream, as in your example, you just need to modify the super call to be correct. For instance:

 import threading class MyClass(threading.Thread): def __init__(self,par1,par2): super(MyClass, self).__init__() self.var1 = par1 self.var2 = par2 def run(self): #do stuff with var1 and var2 while conditions are met 

The init call is already being sent to it, so when you provide it again, it sets another argument in the constructor of the Thread class, and everything gets confused.

+2
source

Source: https://habr.com/ru/post/1443558/


All Articles