我有以下脚本(不参考内容):
import _thread
def func1(arg1, arg2):
print("Write to CLI")
def verify_result():
func1()
for _ in range (4):
_thread.start_new_thread(func1, (DUT1_CLI, '0'))
verify_result()
我想同时执行(比如4个线程)func1()在我的例子中,其中包括一个可能需要时间执行的函数调用。然后,只有在最后一个线程完成其工作后我才想执行验证结果().
目前,我得到的结果是所有线程都完成了它们的工作,但是验证结果()在所有线程完成其工作之前执行。
我什至尝试在 for 循环下使用以下代码(当然我导入了线程),但这没有完成工作(不要引用参数)
t = threading.Thread(target = Enable_WatchDog, args = (URL_List[x], 180, Terminal_List[x], '0'))
t.start()
t.join()
你最后一次threading
示例很接近,但是您必须将线程收集在列表中,立即启动它们,然后等待它们立即完成。这是一个简化的示例:
import threading
import time
# Lock to serialize console output
output = threading.Lock()
def threadfunc(a,b):
for i in range(a,b):
time.sleep(.01) # sleep to make the "work" take longer
with output:
print(i)
# Collect the threads
threads = []
for i in range(10,100,10):
# Create 9 threads counting 10-19, 20-29, ... 90-99.
thread = threading.Thread(target=threadfunc,args=(i,i+10))
threads.append(thread)
# Start them all
for thread in threads:
thread.start()
# Wait for all to complete
for thread in threads:
thread.join()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)