Unten ist die Python-Codes, Die Dekoration ist in Ordnung ohne Thread durch eine Klasse erben von threading.thread erstellt. zum Beispiel einen Thread erstellen, indem Sie das Ziel func als paremeter geben threading.thread()Kann die Parameter eines Func nicht mit threading lib in einer Python-Dekoration erhalten?
import threading ,time
from time import sleep, ctime
import functools
def find(func):
@functools.wraps(func)
def wrapper(*args,**kwargs):
print("ags:%s,%s\n" % (args,kwargs))
return func(*args, **kwargs)
return wrapper
@find
def now() :
return str(time.strftime('%Y-%m-%d %H:%M:%S' , time.localtime()))
class myThread (threading.Thread) :
"""docstring for myThread"""
@find
def __init__(self, nloop, nsec) :
super(myThread, self).__init__()
self.nloop = nloop
self.nsec = nsec
@find
def run(self):
print('start loop', self.nloop, 'at:', ctime())
sleep(self.nsec)
print('loop', self.nloop, 'done at:', ctime())
@find
def main():
thpool=[]
print('starting at:', now())
for i in range(10):
thpool.append(myThread(i,2))
for th in thpool:
th.start()
for th in thpool:
th.join()
print('all Done at:', now())
if __name__ == '__main__':
main()
ich einen Fehler info wie unten bekam:
File "F:\question\multithreadfmclass.py", line 15, in wrapper
print("ags:%s,%s\n" % (args,kwargs))
File "D:\ProgramFiles\Python352\lib\threading.py", line 813, in __repr__
assert self._initialized, "Thread.__init__() was not called"
AssertionError: Thread.__init__() was not called
Wie die Bugs entfernen? Tks im Voraus.