Skip to content

Commit 9bc8f7c

Browse files
Merge pull request #893 from adamtheturtle/custom-wait-between-requests
Custom wait between requests
2 parents 0e4516a + c50b6d5 commit 9bc8f7c

File tree

2 files changed

+53
-5
lines changed

2 files changed

+53
-5
lines changed

src/vws/vws.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -307,13 +307,22 @@ def _wait_for_target_processed(
307307
sleep(seconds_between_requests)
308308

309309
@timeout_decorator.timeout(seconds=60 * 5)
310-
def wait_for_target_processed(self, target_id: str) -> None:
310+
def wait_for_target_processed(
311+
self,
312+
target_id: str,
313+
seconds_between_requests: float = 0.2,
314+
) -> None:
311315
"""
312316
Wait up to five minutes (arbitrary) for a target to get past the
313317
processing stage.
314318
315319
Args:
316320
target_id: The ID of the target to wait for.
321+
seconds_between_requests: The number of seconds to wait between
322+
requests made while polling the target status.
323+
We wait 0.2 seconds by default, rather than less, than that to
324+
decrease the number of calls made to the API, to decrease the
325+
likelihood of hitting the request quota.
317326
318327
Raises:
319328
~vws.exceptions.AuthenticationFailure: The secret key is not
@@ -325,10 +334,6 @@ def wait_for_target_processed(self, target_id: str) -> None:
325334
~vws.exceptions.UnknownTarget: The given target ID does not match a
326335
target in the database.
327336
"""
328-
# We wait 0.2 seconds rather than less than that to decrease the
329-
# number of calls made to the API, to decrease the likelihood of
330-
# hitting the request quota.
331-
seconds_between_requests = 0.2
332337
self._wait_for_target_processed(
333338
target_id=target_id,
334339
seconds_between_requests=seconds_between_requests,

tests/test_vws.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,49 @@ def test_default_seconds_between_requests(
321321
)
322322
assert report['request_usage'] == expected_requests
323323

324+
def test_custom_seconds_between_requests(
325+
self,
326+
high_quality_image: io.BytesIO,
327+
) -> None:
328+
"""
329+
It is possible to customize the time waited between polling requests.
330+
"""
331+
with MockVWS(processing_time_seconds=0.5) as mock:
332+
database = VuforiaDatabase()
333+
mock.add_database(database=database)
334+
client = VWS(
335+
server_access_key=database.server_access_key.decode(),
336+
server_secret_key=database.server_secret_key.decode(),
337+
)
338+
339+
target_id = client.add_target(
340+
name='x',
341+
width=1,
342+
image=high_quality_image,
343+
)
344+
345+
client.wait_for_target_processed(
346+
target_id=target_id,
347+
seconds_between_requests=0.3,
348+
)
349+
report = client.get_database_summary_report()
350+
expected_requests = (
351+
# Add target request
352+
1 +
353+
# Database summary request
354+
1 +
355+
# Initial request
356+
1 +
357+
# Request after 0.3 seconds - not processed
358+
# This assumes that there is less than 0.2 seconds taken
359+
# between the start of the target processing and the start of
360+
# waiting for the target to be processed.
361+
1 +
362+
# Request after 0.6 seconds - processed
363+
1
364+
)
365+
assert report['request_usage'] == expected_requests
366+
324367

325368
class TestGetDuplicateTargets:
326369
"""

0 commit comments

Comments
 (0)