优化批量SSL API请求的速度

2024-10-04 09:28:24 发布

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

我已经编写了一个脚本来运行大约530个API调用,我打算每5分钟运行一次,从这些调用中我存储数据以便以后批量处理(预测等)

API限制每秒500个请求。然而,当运行我的代码时,我每次调用都会看到2秒的时间(我相信是由于SSL)

如何加快速度,使我能够在5分钟内运行500个请求,因为当前所需的时间使我收集的数据变得无用:(

代码:

def getsurge(lat, long):

    response = client.get_price_estimates(
                    start_latitude=lat,
                    start_longitude=long,
                    end_latitude=-34.063676,
                    end_longitude=150.815075
            )

    result = response.json.get('prices')

    return result

def writetocsv(database):
    database_writer = csv.writer(database)
    database_writer.writerow(HEADER)

    pool = Pool()

# Open Estimate Database
    while True:

        for data in coordinates:
            line = data.split(',')

            long = line[3]
            lat = line[4][:-2]

            estimate = getsurge(lat, long)

            timechecked = datetime.datetime.now()

            for d in estimate:
                if d['display_name'] == 'TAXI':
                    database_writer.writerow([timechecked, [line[0], line[1]], d['surge_multiplier']])
                    database.flush()
                    print(timechecked, [line[0], line[1]], d['surge_multiplier']) 

Tags: 数据代码apigetresponsedefline时间