DenisCarriere / geocoder

:earth_asia: Python Geocoder
http://geocoder.readthedocs.org
MIT License
1.62k stars 286 forks source link

"Too many open files" error #439

Open ianbstewart opened 3 years ago

ianbstewart commented 3 years ago

After using geocoder.osm for a few hundred queries, I get the same error over and over from geocoder:

ERROR - HTTPSConnectionPool(host='nominatim.openstreetmap.org', port=443): Max retries exceeded with url: /sea
rch?q=NYC&format=jsonv2&addressdetails=1&limit=1 (Caused by SSLError(OSError(24, 'Too many open files')))

I don't think that the problem is on my end. It seems like a weird version of rate limiting. I added a sleep function for 5 seconds between queries but that didn't change anything. How to fix this error?

interDist commented 2 years ago

I was investigating a different warning, but I think the root cause is the same: Geocoder does not explicitly close the HTTP session it makes use of: https://github.com/DenisCarriere/geocoder/blob/39b9999ec70e61da9fa52fe9fe82a261ad70fa8b/geocoder/base.py#L389 It is by design, since it can reuse a session created elsewhere. Even though I would expect it to close itself the session it creates on its own or at least clearly document this in the docs (the section “Using a Session” has no mention of the fact that Geocoder will create a session if one is not provided in the kwargs).

The solution is to explicitly close the session after you’ve retrieved the results:

    result = geocoder.osm( ... )
    result.session.close()