1

I have a method which receives multiple url strings and is supposed to create HTTP requests for each of them, simultaneously, wait for them to finish and then return a list containing the result of each request.

I tried to implement the async/await logic as illustrated with the code below:

private static readonly HttpClient httpClient = new HttpClient();

public class Response
{
    public bool isError;
    public string content;
}

public async void TestManyGetRequestsAsync(List<string> urls)
{
    List<Response> responses = await GetManyRequestsAsync(urls);
    foreach (Response response in responses)
    {
        Debug.LogFormat("Content: {0}", response == null ? "NULL" : response.content);
    }
}

async Task<List<Response>> GetManyRequestsAsync(List<string> urls)
{
    List<Task<Response>> tasks = new List<Task<Response>>();

    foreach (string url in urls)
    {
        Task<Response> task = Task.Run<Response>(() => AsyncGetRequest(url)); // This line appears to do the same thing as the line below
        // Task<RestAPI.Response> task = RestAPI.Get(url);
        tasks.Add(task);
    }

    Response[] responses = await Task.WhenAll(tasks);
    return responses.ToList();
}

public async Task<Response> AsyncGetRequest(string url)
{
    Task<HttpResponseMessage> task = httpClient.GetAsync(url);
    HttpResponseMessage message;

    Debug.LogFormat("Sending GET request for {0}", url);
    try
    {
        message = await task;
    }
    catch
    {
        return null;
    }

    Response response = new Response {
        isError = !message.IsSuccessStatusCode,
        content = await message.Content.ReadAsStringAsync()
    };

    Debug.LogFormat("Received GET response for {0}: {1}", url, response);
    return response;
}

The issue is that every task is running sequentially, meaning that each task is on hold while the previous is in progress.

I have the impression that different factors may be involved (the way I implemented the async/await logic, the way requests are run, etc.) but I am not able to figure out a clear solution to my issue.

Any hint or suggestion would be highly appreciated, thanks!

thompsoff
  • 11
  • 1
  • I would suggest to rather use [`UnityWebRequest.Get`](https://docs.unity3d.com/ScriptReference/Networking.UnityWebRequest.Get.html) .. then you can simply [Wait for all Coroutines to finish](https://stackoverflow.com/questions/58827893/wait-for-all-requests-to-continue) – derHugo Jan 21 '21 at 09:04

1 Answers1

0

It might be easier to use Unity's own UnitWebRequest, it's async by nature so all you have to do is start them all, and then check every frame to see if they are done. This simple code below demonstrates this, you can improve performance by using coroutines but it's probably not necessary unless you're handling a very large number of web calls concurrently.

    string[] urls = { "http://url1.com", "http://url2.com" };
    List<UnityWebRequest> webRequests = null;
    List<UnityWebRequest> doneRequests = null;
    bool initialized = false;
    private void Update() {
        InitWebRequests();
        if (CheckWebRequestsDone()) {
            HandleResults(doneRequests);
        }
    }

    private void InitWebRequests() {
        if (initialized) {
            return;
        }

        initialized = true;
        webRequests = new List<UnityWebRequest>();
        foreach (string url in urls) {
            UnityWebRequest wr = UnityWebRequest.Get(url);
            wr.SendWebRequest();
            webRequests.Add(wr);
        }
    }

    private bool CheckWebRequestsDone() {
        foreach(UnityWebRequest wr in webRequests.ToArray()) {
            if (wr.isDone) {
                webRequests.Remove(wr);
                doneRequests.Add(wr);
            }
        }

        return webRequests.Count == 0;
    }
tbkn23
  • 5,205
  • 8
  • 26
  • 46