UniTask icon indicating copy to clipboard operation
UniTask copied to clipboard

Limiting Threads and/or Task

Open haitheredavid opened this issue 3 years ago • 3 comments

Hello! 🧇

I've been integrating UniTask into a package of mine but I've been hitting some issues with memory allocation, and I wanted to see if you could toss some knowledge my way. This might not have anything to do with UniTask, but the bit of googling hasn't helped.

I'm hitting an issue with pulling a large object down from an API. The API has a async helper function that does all the setup and REST calls, returning a Task<>. I tagged that helper function with AsUniTask() and that works perfectly. However, whenever I use the API function my CPU immediately hits 100% on all cores and blocks Unity until the CPU has dropped down.

😢 image

Is there a way to throttle the CPU with UniTask? The bit of digging showed that I could use TaskPool.SetMaxThreadSize() but that didn't seem to help any. I've replicated the same code I used for my package but filled in the API logic with UniTask.Delay() and that didn't cause a block with unity. I figure this most likely means that the API code needs to be handled differently, but the same code works on different clients and doesn't cause a cpu block like it does with unity.

Here is an example of how that code is setup

haitheredavid avatar May 26 '22 20:05 haitheredavid

What happens if it don't use AsUniTask?

public async UniTask Receive()
{
    // snip ...

    var commit = await client.CommitGet(
        token,
        stream.Id,
        stream.CommitId
    ); // .AsUniTask();

    SpeckleUnity.Console.Log($"obj id ={commit.referencedObject}");

    @base = await Operations.Receive(
        commit.referencedObject,
        token,
        transport
    ); // .AsUniTask();

    // snip ...
}

neuecc avatar May 27 '22 07:05 neuecc

yeah, that fixed it! Thank you. Still having a high CPU usage but unity doesn't get blocked by that. I thought converting a Task with ToUniTask would be the solution to heavy tasks in unity. Maybe because there is multiple Tasks within those API calls?

haitheredavid avatar May 27 '22 12:05 haitheredavid

@neuecc Bump 🤓!

Something I was thinking is that converting a task to unitask might create overhead for large API REST calls

haitheredavid avatar Jun 28 '22 20:06 haitheredavid

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 7 days.

github-actions[bot] avatar Sep 27 '22 00:09 github-actions[bot]