循环调用某接口,发现开启的线程一些没运行,一些中断了,原来是没加锁,加锁之后OK了(使用join方法的作用是调用线程等待该线程完成后,才能继续用下运行,相当于单线程了)
# coding:utf-8
import threading
import time
def action(arg):
try:
threadLock.acquire()
....
finally:
threadLock.release()
threadLock = threading.Lock()
l=[]
for i in range(4):
t=threading.Thread(target=action,args=(i,))
l.append(t)
for i in l:
i.start()
python线程限制并发数: