Python请求 - WinError 10048套接字地址的一种用法

时间:2016-02-26 13:51:45

标签: python multithreading python-requests

我有一个脚本似乎运行良好的一台计算机,但没有另一台。我启动了一个osrm-routing实例(本地服务器来获取驱动器距离)和多线程请求:

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..")

1 个答案:

答案 0 :(得分:0)

正如评论所述:

$a = array(/*some data*/);
$b = array(/*some data*/);
$c = array(/*some data*/);

$out = array();
$j = 0; //index of output array - table row
for($i = 0; $i<count($a); i++) {
    if($a[i] == 1) {
        $out[j][0] = $a[i];
        if($b[$i+1] == 1) {
            $out[j][1] = $b[$i+1];
        }
        $j++;
    } else {
        if($c[$i] == 1) {
            $out[j][2] = $c[$i];
        }
        $j++;
    }
}

buildTable($out); //now you can build nice html array, 
                // or send data in json to some API
                // e.g. to build this table dynamically with javascript 

而不是

pool = Pool(int(cpu_count()-1)