Skip to content
This repository has been archived by the owner on Feb 16, 2024. It is now read-only.

How to convert this to a WhenAll pattern? #523

Closed
JPhilipp opened this issue Oct 25, 2022 · 2 comments
Closed

How to convert this to a WhenAll pattern? #523

JPhilipp opened this issue Oct 25, 2022 · 2 comments

Comments

@JPhilipp
Copy link

JPhilipp commented Oct 25, 2022

Great library! What's the right way to convert something like the following (this example uses a GPT-3 www request, but it could be anything)

string result1 = await textAI.GetCompletion("Albeit Einstein was");
string result2 = await textAI.GetCompletion("Susan Sarandon is");

to something that launches the GetCompletion functions simultaneously, then continues when all finished and returned their result? E.g. using pseudo-code, like this:

string result1 = null;
string result2 = null;

await Task.WhenAll({
    result1 = await textAI.GetCompletion("Albeit Einstein was"),
    result2 = await textAI.GetCompletion("Susan Sarandon is")
});

Debug.Log("Results: " + result1 + ", " + result2);

Thanks!

@JPhilipp
Copy link
Author

JPhilipp commented Oct 25, 2022

Just realized (thanks to Evert at StackOverflow) that UniRx isn't needed in this project nowadays. The following works:

using UnityEngine;
using System;
using System.Net.NetworkInformation;
using System.Threading;
using System.Threading.Tasks;
// ... snip ...

async void TestWhenAll()
{
    Task<string> a = textAI.GetCompletion("Albert Einstein was", useCache: false);
    Task<string> b = textAI.GetCompletion("Susan Sarandon is",   useCache: false);
    
    await Task.WhenAll(a, b);
    
    Debug.Log("a: " + a.Result);
    Debug.Log("b: " + b.Result);
}

@elhimp
Copy link

elhimp commented Oct 25, 2022

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants