python多线程调用接口
场景:有百万地址需要调用接口
设计:用pandas读取地址。转成list,由于list在多线程存在线程安全问题,遍历list存到队列中。多线程去队列中取数据进行调用接口。代码如下
设计:用pandas读取地址。转成list,由于list在多线程存在线程安全问题,遍历list存到队列中。多线程去队列中取数据进行调用接口。代码如下
# *_*coding:utf-8 *_* # @Author : zyb import pandas,time,threading,queue,requests exl = pandas.read_excel(r'C:\Users\Administrator\Desktop\66.xlsx',index_col=None) address_list = exl['address'].to_list() address_queue =queue.Queue() res_queue =queue.Queue() start_time ='2022-7-09 23:59:59'#开始计时时间 url = 'xxxxxxxxxxx' for address in address_list: address_queue.put(address) def send_api(): while True: if address_queue.qsize()>0 and res_queue.qsize()<500000: address = address_queue.get() try: res = requests.get(url=url+address) if int(time.time())>=int(time.mktime(time.strptime(start_time, "%Y-%m-%d %H:%M:%S"))): if int(res.json()['data']['isThirdFlag']) >= 0: res_queue.put(address) except: print('未获取到isThirdFlag字段') else: break print('总数据:{}条,剩余{}条'.format(str(len(address_list)),str(address_queue.qsize()))) threads = [] for t in range(20): threads.append(threading.Thread(target=send_api, args=())) if __name__=="__main__": for thr in threads: thr.start() for thr in threads: thr.join()