1
以下はPythonのコードです。装飾は、スレッドを通してスレッドを作成せずにOKです。クラスから継承します。バグを削除する方法Pythonデコレーションでスレッドlibを使ってfuncのパラメータを取得できませんか?
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
:例えば(threading.threadするparemeterとしてターゲットfuncを与えることによって、スレッドを作成)
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()
私は以下のようにエラー情報を得ましたか。事前にtks。