我正在尝试使用对内部系统API的API调用来获取多个客户机的数据(csv文件的详细信息)。目前,它显示平均超过1000个API调用,运行15个线程时的响应时间是0.6691秒。在50万个请求的过程中,加起来大约93个小时。我的代码没有太多API细节,如下所示:
def get_file():
count = -1
freader = open(f_name, 'rU')
csvreader = csv.reader(freader)
for row in csvreader:
userid = str(row[0])
count += 1
while (activeCount() > 15):
time.sleep(10)
continue
thread = Thread(target=check, args=(userid, count,))
thread.start()
# check(userid)
thread.join()
def check(userid, count):
headers = {
'Accept': '*/*',
'Authorization': authStr,
'accept-encoding': 'gzip, deflate'
}
url = "{}{}/{}/{}".format(api_url, site_id, id_type, userid)
response = requests.get(url, headers=headers)
if response.status_code == 404:
viewed_count = 0
else:
viewed_count = json_extract(response.json())
我怎样才能加快速度?我可以指定的threads(activeCount)
的最大数目是多少?有没有一种更简单、更快、更优雅的方式来做到这一点?在
虽然Python中的线程由于GIL限制而不能同时运行(旁注),但是线程化可能会有所帮助,因为响应阻塞了当前线程,但不需要计算,因此线程被设置为休眠。
在此期间,另一个线程可以发出请求。
试着找出你想要提出请求的线程的最佳位置。在
相关问题 更多 >
编程相关推荐