当我将我的方法移动到一个类中时,logging.debug停止了打印thread-#,它现在只打印mainthread。如何让logging.debug为每个日志打印thread-#
代码:
class threadTest():
testResult=''
def __init__(self,nThread,nTests,debugOn):
self.nThread = nThread
self.nTests = nTests
self.debugOn = debugOn
if debugOn == 1:
logging.basicConfig(level=logging.DEBUG,format='[%(levelname)s] (%(threadName)-10s) %(message)s',)
@classmethod
def test(cls):
if debugOn == 1: logging.debug('Starting')
...do something...
if debugOn == 1: logging.debug('Exiting')
time.sleep(1)
arguments=sys.argv
nThread = int(sys.argv[1]) if len(sys.argv)>1 else 1
nTests = int(sys.argv[2]) if len(sys.argv)>2 else 1
debugOn = int(sys.argv[3]) if len(sys.argv)>3 else 0
myThread = threadTest(nThread,nTests,debugOn)
x=1
while x <= nThread:
thread='Thread-'+str(x)
t=threading.Thread(name=thread, target=myThread.test())
t.start()
x+=1
time.sleep(1)
当前输出:
[DEBUG] (MainThread) Starting
[INFO] (MainThread) Starting new ...
[DEBUG] (MainThread) Exiting
[DEBUG] (MainThread) Starting
[INFO] (MainThread) Starting new ...
[DEBUG] (MainThread) Exiting
预期产出:
[DEBUG] (Thread-1) Starting
[INFO] (Thread-1) Starting new ...
[DEBUG] (Thread-1) Exiting
[DEBUG] (Thread-2) Starting
[INFO] (Thread-2) Starting new ...
[DEBUG] (Thread-2) Exiting
答案 0 :(得分:0)
@tdelaney的Thx指出问题所在。
t=threading.Thread(name=thread, target=myThread.test())
已更改为
t=threading.Thread(name=thread, target=myThread.test)
线程-1,-2 ....现在出现