python-3.x - 有没有办法增加我可以发送 Docker 的请求数量?
问题描述
目前我正在使用 python 构建一个小型应用程序,该应用程序允许我查看从一个位置到其他位置的旅行时间。大约 50k 其他位置。我正在使用 docker 设置一个 API 沙箱来运行 OSRM 计算,该计算将返回估计的旅行时间。
我以前从未使用过 Docker,所以这对我来说是全新的。在 Docker 阻止我之前,我可以发送大约 12-14k 个请求。
现在我可以轻松地拼接我的数据库,以 10k 的数据包发送我的请求。但是,我想知道是否有一种方法可以让 docker 接受完整的 50K 请求而不会将我踢出去。
这是我得到的错误。希望这可以澄清问题,因为我不确定我是否正确地问了这个问题。
HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /route/v1/driving/-104.7783563744188,40.705742844393505;-104.8130275,40.0847055 (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at 0x000001786C29E708>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted'))
例子:
import polyline
import requests
import numpy as np
def get_route(pickup_lat, pickup_lon, dropoff_lat, dropoff_lon):
'''Outputs the respecting route characteristics.
distance, duration, route'''
# Configure URL request for docker and correct port
loc = "{},{};{},{}".format(pickup_lon, pickup_lat, dropoff_lon, dropoff_lat)
url = "http://127.0.0.1:5000/route/v1/driving/"
with requests.Session() as s:
r = s.get(url + loc)
if r.status_code!= 200:
return {}
# Decoding the Json file
res = r.json()
routes = polyline.decode(res['routes'][0]['geometry'])
start_point = [res['waypoints'][0]['location'][1], res['waypoints'][0]['location'][0]]
end_point = [res['waypoints'][1]['location'][1], res['waypoints'][1]['location'][0]]
distance = res['routes'][0]['distance']
duration = res['routes'][0]['duration']
# Putput the relative metrics
out = {'distance':distance,
'duration':duration}
return out
lon = np.random.uniform(low=-105, high=-104, size=(50000))
lat = np.random.uniform(low=40, high=41, size=(50000))
df = np.vstack((lat, lon)).T
index = 0
for lat_lon in df:
get_route(*lat_lon, 40.0847055, -104.8130275)
print(index)
index += 1
与 requests.Sessions 似乎工作得更糟或更不一致。范围在 3-15k 请求之间。
编辑:kthompso 的建议已经解决了。将 with.sessions 带出功能是解决方案
with requests.Session() as s:
for lat_lon in df:
get_route(*lat_lon, 40.0847055, -104.8130275)
print(index)
index += 1
解决方案
推荐阅读
- magento2 - Magento Paypal 选项无法通过卡支付,没有贝宝账户
- android - 如何使用 Appium 向 Android 设备输入非 ASCII 文本?
- python - 无法让我的井字游戏在 python 中正确运行
- php - 如何在控制器中构建 div 并放置在需要的页面上
- sql - 如何在交叉表查询的多个列中计算一条记录?
- android - 提交评论时列表视图未显示正确信息
- elasticsearch - 向 ElasticSearch 前缀查询添加模糊性
- forms - 是否可以将表单输入嵌入到富文本编辑器文档模型中?
- ironpython - Iron Python如何在Table或CrossTable中左对齐或居中
- python - 为什么我的鼠标按钮没有在 pygame 中注册?