我有一个脚本,它似乎在一台计算机上运行良好,但在另一台计算机上运行不了。我启动了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)
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]
但是,在其中一台计算机上,我有时会收到此错误:
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...
如果我在浏览器中手动输入 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..")
如评论中所述:
pool = Pool(int(cpu_count()-1)
而不是
pool = Pool() # all threads