t1 怎么可以多次调用呢?
import time
import threading
def end():
time.sleep(2)
print('完成')
t1 = threading.Thread(target=end)
def start():
print('开始')
for i in range(5):
for r in range(5):
time.sleep(1)
print(r)
t1.start()
start()
开始
0
1
2
3
4
0
完成
1
2
3
4
Traceback (most recent call last):
File "D:\Code\NewWorld\Test1.py", line 18, in
start()
File "D:\Code\NewWorld\Test1.py", line 16, in start
t1.start()
File "C:\Users\XXX\AppData\Local\Programs\Python\Python310\lib\threading.py", line 923, in start
raise RuntimeError("threads can only be started once")
RuntimeError: threads can only be started once
望采纳,可以这样改
import time
import threading
def end():
time.sleep(2)
print('完成')
def start():
print('开始')
for i in range(5):
t1 = threading.Thread(target=end)
for r in range(5):
time.sleep(1)
print(r)
t1.start()
start()