Python请求-WinError 10048套接字地址的一种用法
我有一个脚本,它在一台计算机上运行良好,但在另一台计算机上运行不好。我启动一个osrm路由实例(获取驱动器距离的本地服务器)和多线程请求:Python请求-WinError 10048套接字地址的一种用法,python,multithreading,python-requests,Python,Multithreading,Python Requests,我有一个脚本,它在一台计算机上运行良好,但在另一台计算机上运行不好。我启动一个osrm路由实例(获取驱动器距离的本地服务器)和多线程请求: def ReqOsrm(url_input): url_to_geocode, query_id = url_input try_c = 0 while try_c < 5: try: response = requests.get(url_to_geocode)
def ReqOsrm(url_input):
url_to_geocode, query_id = url_input
try_c = 0
while try_c < 5:
try:
response = requests.get(url_to_geocode)
json_geocode = response.json()
status = int(json_geocode['status'])
# Found route between points
if status == 200:
tot_time_s = json_geocode['route_summary']['total_time']
tot_dist_m = json_geocode['route_summary']['total_distance']
used_from = json_geocode['via_points'][0]
used_to = json_geocode['via_points'][1]
out = [query_id,
status,
tot_time_s,
tot_dist_m,
used_from[0],
used_from[1],
used_to[0],
used_to[1]]
return out
# Cannot find route between points (code errors as 999)
else:
print("Failed: %d %s" % (query_id, url_to_geocode))
return [query_id, 999, 0, 0, 0, 0, 0, 0]
except Exception as err:
print("%s - retrying..." % err)
time.sleep(5)
try_c += 1
print("Failed: %d %s" % (query_id, url_to_geocode))
return [query_id, 999, 0, 0, 0, 0, 0, 0]
def请求请求(url\u输入):
url\u到\u地理编码,查询\u id=url\u输入
试试c=0
当您尝试使用c<5时:
尝试:
response=requests.get(url到地理编码)
json_geocode=response.json()
status=int(json_地理代码['status'])
#找到点之间的路线
如果状态==200:
tot_time_s=json_地理编码['route_summary']['total_time']
tot_dist_m=json_地理编码['route_summary']['total_distance']
used_from=json_地理编码['via_points'][0]
used_to=json_地理编码['via_points'][1]
out=[query\u id,
地位
总时间,
总区,
已从[0]中使用\u,
从[1]中使用,
用于[0],
用于[1]]
返回
#找不到点之间的路线(代码错误为999)
其他:
打印(“失败:%d%s%”(查询\u id、url到\u地理编码))
返回[查询id,999,0,0,0,0,0,0]
除异常作为错误外:
打印(“%s-重试…”%err)
时间。睡眠(5)
试试看c+=1
打印(“失败:%d%s%”(查询\u id、url到\u地理编码))
返回[查询id,999,0,0,0,0,0,0]
但是,在其中一台计算机上,我有时会出现以下错误:
HTTPConnectionPool(host='127.0.0.1', port=5000): Max retries exceeded with url: /viaroute?loc=49.34343,3.30199&loc=49.56655,3.25837&alt=false&geometry=false (Caused by NewConnectionError('<requests.packages.urllib3.connection.HTTPConnection object at 0x0000005E84FE9B70>: Failed to establish a new connection: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted',)) - retrying...
HTTPConnectionPool(host='127.0.0.1',port=5000):url超过最大重试次数:/viaproute?loc=49.3434343,3.30199&loc=49.56655,3.25837&alt=false&geometry=false(由NewConnectionError(':未能建立新连接:[WinError 10048]每个套接字地址(协议/网络地址/端口)通常只允许一次使用')-重试。。。
如果我在浏览器中手动输入URL,它似乎工作正常,因此我不确定它是否是并行线程问题,但奇怪的是,它仅在一台计算机上存在
我使用POpen像这样启动服务器:
def OsrmServer(self,
osrmport=5005,
osrmip='127.0.0.1'):
try:
p = Popen([self.router_loc, '-v'], stdin=PIPE, stdout=PIPE, stderr=PIPE)
output = p.communicate()[0].decode("utf-8")
except FileNotFoundError:
output = ""
if "info" not in str(output):
raise Exception("OSM does not seem to work properly")
try:
if requests.get("http://%s:%d" % (osrmip, osrmport)).status_code == 400:
raise Exception("osrm-routed already running - force close all with task-manager")
except requests.ConnectionError:
pass
Popen("%s %s -i %s -p %d" % (self.router_loc, self.map_loc, osrmip, osrmport), stdout=PIPE)
try_c = 0
while try_c < 30:
try:
if requests.get("http://%s:%d" % (osrmip, osrmport)).status_code == 400:
return "http://%s:%d" % (osrmip, osrmport)
else:
raise Exception("Map could not be loaded")
except requests.ConnectionError:
time.sleep(10)
try_c += 1
raise Exception("Map could not be loaded ... taking more than 5 minutes..")
def OsrmServer(self,
osrmport=5005,
osrmip='127.0.0.1'):
尝试:
p=Popen([self.router_loc,'-v'],标准输入=管道,标准输出=管道,标准输出=管道)
输出=p.通信()[0]。解码(“utf-8”)
除FileNotFoundError外:
output=“”
如果str中没有“info”(输出):
引发异常(“OSM似乎工作不正常”)
尝试:
如果请求.get(“http://%s:%d”%(osrmip,osrmport))。状态\代码==400:
引发异常(“osrm路由已运行-使用任务管理器强制关闭所有”)
除请求外。ConnectionError:
通过
Popen(“%s%s-i%s-p%d”%(self.router\u loc,self.map\u loc,osrmip,osrmport),stdout=PIPE)
试试c=0
当试着用c<30时:
尝试:
如果请求.get(“http://%s:%d”%(osrmip,osrmport))。状态\代码==400:
返回“http://%s:%d”%(osrmip,osrmport)
其他:
引发异常(“无法加载映射”)
除请求外。ConnectionError:
时间。睡眠(10)
试试看c+=1
引发异常(“无法加载映射…需要5分钟以上…”
如注释中所述,由以下人员解决:
pool = Pool(int(cpu_count()-1)
而不是
pool = Pool() # all threads
您的请求可能是一个线程,但您的
服务器回答不是一个线程。看看这个:return“http://%s:%d”%(osrmip,osrmport)
handle with main thread。这似乎解决了这个问题:pool=pool(int(cpu\u count()-1)),而不是使用我留下的所有线程来给服务器更多的容量。我不确定它有多健壮?是否最好有cpu_计数/2@mptevsion你应该写下来作为一个答案,当系统允许你(在2天内)你应该接受它。