加速pythonapi请求

2024-03-29 14:46:20 发布

您现在位置:Python中文网/ 问答频道 /正文

大家好,我正在尝试将输入从csv文件传递到我的url,该文件包含数百万条记录,因此响应非常慢。这需要很多时间,有时会超时。有人能帮我加快速度吗?我可以提供代码,但不能提供数据和url,因为这是机密信息。 这是我的密码:

import pandas as pd
import requests
import json
from pandas.io.json import json_normalize
from flatten_json import flatten

 df=pd.read_excel('C:/data1.xlsx',index=None,index_col=None,encoding='UTF-8')
 df.head(5)
 df.shape
 df=df[['NAME','country']]
 df.head(5)

 passwd=b"abc"
 user=b"xxxxx"
 # Make a request to the endpoint using the correct auth values
 auth_values = (user,passwd)
 dd=df.values

 dfj = pd.DataFrame()

 for i,j in dd:

     url='http:xyz.com/&name='+str(i)+'&country='+str(j)    
     resp = requests.get(url,auth=auth_values)
     r=resp.json()

请修改此代码以使其更快

提前谢谢你的帮助

enter code here

Tags: 文件代码fromimportnoneauthjsonurl
1条回答
网友
1楼 · 发布于 2024-03-29 14:46:20

试试这个来穿线

from concurrent.futures import ThreadPoolExecutor
executors = ThreadPoolExecutor(max_workers = n) # n = int number of threads.
running_threads = []
#To do a job
run_threads = executors.submit(func,foo) #func is your function
running_threads.append(run_threads)
#To wait for your thread to end:
while True: #All inserts are complete
    if all(i.done() == True for i in running_threads):
        print("all done")
        break
    else:
        time.sleep(1)

相关问题 更多 >