Spaces:
Running
Running
Paul-Edouard Sarlin
commited on
Add retries on RemoteProtocolError and ReadError (#33)
Browse files* Add retries on RemoteProtocolError
* Also retry on ReadError
maploc/data/mapillary/download.py
CHANGED
@@ -20,6 +20,18 @@ semaphore = asyncio.Semaphore(100) # number of parallel threads.
|
|
20 |
image_filename = "{image_id}.jpg"
|
21 |
info_filename = "{image_id}.json"
|
22 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
|
24 |
class MapillaryDownloader:
|
25 |
image_fields = (
|
@@ -50,10 +62,11 @@ class MapillaryDownloader:
|
|
50 |
def __init__(self, token: str):
|
51 |
self.token = token
|
52 |
self.client = httpx.AsyncClient(
|
53 |
-
transport=httpx.AsyncHTTPTransport(retries=20), timeout=
|
54 |
)
|
55 |
self.limiter = AsyncLimiter(self.max_requests_per_minute // 2, time_period=60)
|
56 |
|
|
|
57 |
async def call_api(self, url: str):
|
58 |
async with self.limiter:
|
59 |
r = await self.client.get(url)
|
|
|
20 |
image_filename = "{image_id}.jpg"
|
21 |
info_filename = "{image_id}.json"
|
22 |
|
23 |
+
def retry(times, exceptions):
|
24 |
+
def decorator(func):
|
25 |
+
async def wrapper(*args, **kwargs):
|
26 |
+
attempt = 0
|
27 |
+
while attempt < times:
|
28 |
+
try:
|
29 |
+
return await func(*args, **kwargs)
|
30 |
+
except exceptions:
|
31 |
+
attempt += 1
|
32 |
+
return await func(*args, **kwargs)
|
33 |
+
return wrapper
|
34 |
+
return decorator
|
35 |
|
36 |
class MapillaryDownloader:
|
37 |
image_fields = (
|
|
|
62 |
def __init__(self, token: str):
|
63 |
self.token = token
|
64 |
self.client = httpx.AsyncClient(
|
65 |
+
transport=httpx.AsyncHTTPTransport(retries=20), timeout=120.0
|
66 |
)
|
67 |
self.limiter = AsyncLimiter(self.max_requests_per_minute // 2, time_period=60)
|
68 |
|
69 |
+
@retry(times=5, exceptions=(httpx.RemoteProtocolError, httpx.ReadError))
|
70 |
async def call_api(self, url: str):
|
71 |
async with self.limiter:
|
72 |
r = await self.client.get(url)
|