python - 加快python API请求
问题描述
大家好,我正在尝试将输入从 csv 文件传递到我的 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
解决方案
试试这个线程。
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)
推荐阅读
- c++ - 将引用作为参数传递给类的正确方法
- javascript - Google Maps API 必须点击两次标记才能动态显示图像
- c# - 使用 Source Link 从本地 .snupkg 下载源码进行调试
- c++ - 引用成员引用另一个成员是否合法?
- regex - 正则表达式捕获 IP 地址 (S,G) 多播
- r - 更改 Sankey 中节点的位置
- python - 将 ScrollHandDrag 表单左键单击更改为中键单击 PYQT5
- keras - 损失函数不会降低,并且在二进制 softmax 上给出 50% 的准确度和输出
- aqueduct - 可选查询参数 aqueduct
- python - 如何使用正则表达式解析日志文件中多行的文本?