项目场景:python pyqt5 threading 多线程 数据混乱
根据配置文件多线程调用接口
 # 问题描述: 
参考http://www.starky.ltd/2019/09/23/pro-python-concurrency-with-multi-threading/ 
  
多线程调用接口,取得数据回调修改页面上的值发生混乱不准
 

 # 原因分析: 
运行线程数量过多没有顺序
 # 解决方案: 
可以设置 线程独立对象 obj = threading.local() 直接在方法里 obj 存数据
修改之前
for i in job:
   t = threading.Thread(target=get, args=(i, call, nloops, parentWindows))
   threads.append(t)
   nloops = nloops + 1
nloops = range(nloops)
for i in nloops:
   time.sleep(2)
   threads[i].start()  # 开启线程
for i in nloops:  # 等待
   time.sleep(2)
   threads[i].join()  # 线程完成修改之后
改成线程池,设定固定数量线程对象
THREAD_POOL_SIZE = 4
def worker(work_queue):
    while not work_queue.empty():
        try:
            item = work_queue.get(block=False)
        except Empty:
            break
        else:
            get(item["i"],item["call"],item["nloops"],item["parentWindows"],item["result"])
            work_queue.task_done()
def run_job(job=None, work_url=None, token=None, call=None, main_call_back=None, parentWindows= None):
    try:
        g.gz_arr = []
        work_queue = Queue()
        nloops = 0
        for i in job:
            work_queue.put({"i":i, "call":call, "nloops":nloops, "parentWindows":parentWindows, "result":""})
            nloops = nloops + 1
        threads = [
            threading.Thread(target=worker, args=(work_queue,)) for _ in range(THREAD_POOL_SIZE)
        ]
        for thread in threads:
            thread.start()
        work_queue.join()
        while threads:
            threads.pop().join()                










