为什么我的geopy循环总是以Killed:9结尾?

时间:2019-02-07 18:01:47

标签: python geopy

我有一个地址列表,当我尝试添加坐标时,刚遇到了Kill 9错误。

是否超时?我增加了睡眠时间来防止这种情况。

我收到此错误Killed: 9

def do_geocode(Nominatim, address):
    time.sleep(3)
    try:
        return Nominatim.geocode(address)
    except GeocoderTimedOut:
        return do_geocode(Nominatim,address)

def addCoordinates(businessList):
    businessList[0] = ["pageNum","entryNum","name","address","tagOne","tagTwo","tagThree","geoAddress","appendedLocation","latitude","longitude","key"]

    geolocator = Nominatim(timeout=None)
    z = 0
    i=1
    while i < len(businessList):
        longitude = ""
        latitude = ""
        geoLocation = ""
        geoAddress = ""
        entry = []

        appendedLocation = (businessList[i][3] + ", San Francisco")

        geoLocation = do_geocode(geolocator, appendedLocation)

        if geoLocation is not None:
            geoAddress = geoLocation.address
            latitude = geoLocation.latitude
            longitude = geoLocation.longitude

            entry = [geoAddress, appendedLocation, str(latitude), str(longitude)]
            j=0
            while j < len(entry):
                businessList[i] += [entry[j]]
                j+=1
            print("coordinates added")
            z +=1
            print(z)

        i+=1

1 个答案:

答案 0 :(得分:1)

Killed: 9可能意味着您的Python脚本已被操作系统中的某些内容终止(也许是OOM杀手?)。确保脚本不会占用计算机的全部可用内存。

对于geopy,我建议看一下RateLimiter类。还要注意,使用Nominatim时需要指定唯一的用户代理(在the Nominatim class docs中进行了说明)。你会得到这样的东西:

from geopy.extra.rate_limiter import RateLimiter


def addCoordinates(businessList):
    businessList[0] = ["pageNum","entryNum","name","address","tagOne","tagTwo","tagThree","geoAddress","appendedLocation","latitude","longitude","key"]

    geolocator = Nominatim(user_agent="specify_your_app_name_here", timeout=20)
    geocode = RateLimiter(
        geolocator.geocode, 
        min_delay_seconds=3.0,
        error_wait_seconds=3.0,
        swallow_exceptions=False, 
        max_retries=10,
    )
    z = 0
    i=1
    while i < len(businessList):
        longitude = ""
        latitude = ""
        geoLocation = ""
        geoAddress = ""
        entry = []

        appendedLocation = (businessList[i][3] + ", San Francisco")

        geoLocation = geocode(appendedLocation)

        if geoLocation is not None:
            geoAddress = geoLocation.address
            latitude = geoLocation.latitude
            longitude = geoLocation.longitude

            entry = [geoAddress, appendedLocation, str(latitude), str(longitude)]
            j=0
            while j < len(entry):
                businessList[i] += [entry[j]]
                j+=1
            print("coordinates added")
            z +=1
            print(z)

        i+=1