MicrosoftDocs / azure-docs

Open source documentation of Microsoft Azure
https://docs.microsoft.com/azure
Creative Commons Attribution 4.0 International
10.26k stars 21.43k forks source link

Why are we using Thread.sleep(1000) and is there any better way to get the result other than the poll aproach? #79632

Closed MA1GitHub closed 3 years ago

MA1GitHub commented 3 years ago

[Enter feedback here] Is there any better way to get the result? Thread.sleep(1000) makes it really slow. Like instead of checking if the result is complete in a while, is there any way to for us to know if it's complete without using the while loop? while (pollForResult) { // Poll for result every second Thread.sleep(1000); readResults = vision.getReadResult(UUID.fromString(operationId));

    // The results will no longer be null when the service has finished processing the request.
    if (readResults != null) {
        // Get request status
        OperationStatusCodes status = readResults.status();

        if (status == OperationStatusCodes.FAILED || status == OperationStatusCodes.SUCCEEDED) {
            pollForResult = false;
        }
    }
}

Document Details

Do not edit this section. It is required for docs.microsoft.com ➟ GitHub issue linking.

GiftA-MSFT commented 3 years ago

@MoAGitHub we'll review your feedback and get back to you shortly. Thanks.

GiftA-MSFT commented 3 years ago

Hi @MoAGitHub, an efficient approach may be to use ScheduledExecutorService. Hope this helps.

@PatrickFarley please review above feedback for doc enhancement.

Thanks!

MA1GitHub commented 3 years ago

@GiftA-MSFT Can you please include a sample code on how to use ScheduledExecutorService with the Microsoft Azure api?

GiftA-MSFT commented 3 years ago

@MoAGitHub we don't have a sample code at the moment (we'd have to add this request to our backlog). Reassigning to content author for further review. Thanks.

PatrickFarley commented 3 years ago

I don't know of a way to check the result without polling it. We use a polling timer on many similar APIs. You could always shorten the time of delay to reduce the lag.

please-close

MA1GitHub commented 3 years ago

@PatrickFarley If we reduce the delay time, the billing cost will be more. As you know, Microsoft charges you based on how many calls you make. I wish there was a smarter way to achieve this (rather than to use polling technique). When I tired to reduce the time of the delay on the free tier, it started throwing exceptions and it asked to not make request so frequently (which means to increase the delay time).