WWW request in coroutine is blocking main thread?

I am downloading a resource from my server (approx. 4mb in size) and the download is blocking the main thread. Here’s the minimal code:

// Part of a MonoBehaviour script...
IEnumerator Foo()
{
    // Actual URL removed for privacy...
    WWW w = new WWW("http://myserver.com/myscript.php");
 
    yield return w;

    Debug.Log(w.text);
}

void LoadResource()
{
    StartCoroutine(Foo());
}

void Update()
{
    Debug.Log("Hello");
}

The GUI freezes (can’t even stop playback in the Editor) and the Update doesn’t get called again until the download has finished.

Try a busy wait construct where you yield return null and instead check the .isDone flag on your WWW object… might work better. Something like:

        using( WWW w = new WWW("http://myserver.com/myscript.php"))
        {
            while( !w.isDone)
            {
                yield return null;
            }

            Debug.Log(w.text);
        }

Remember to wrap your WWW (and any other object implementing the IDisposable interface) in a using construct, or else try/finally/.Dispose it manually.

Also coroutines ARE you main thread. They are just special wrappered Update calls. They are not multi threaded methods.

1 Like

Ok, so I have this code:

IEnumerator Foo()
{
    int loopCount = 0;
    using (WWW w = new WWW("server address..."))
    {
        while (!w.isDone)
        {
            Debug.Log(DateTime.Now + ": " + loopCount++);
            yield return null;
        }

        Debug.Log(DateTime.Now + ": " + "loopCount: " + loopCount);
        Debug.Log(w.text);
    }
}

int updateCount;
void LoadResource()
{
    updateCount = 0;
    StartCoroutine(Foo());
}

void Update()
{ 
    Debug.Log(DateTime.Now + ": " + "updateCount: " + updateCount++);
}

However, the update and while debug logs get about 50 iterations but then it freezes until the download is finished. Interestingly, the isDone is set to true just before the freeze, as if it’s finished downloading but it hasn’t actually finished and blocks until it’s finished.

I’m stumped…

btw updateCount is actually initialized to 0 but I can’t edit the thread right now.

Are you sure the chokepoint isn’t cramming 4mb through the Debug.Log() system?? That will tend to “leave a mark” so to speak.

6 Likes

^this guy!

Thanks a bunch, that fixed it up a treat. Works flawlessly now.

1 Like

What’s the fix? I just don’t understand what Kurt means

Debug.Log is relatively slow, and scales poorly. When OP did this:

Debug.Log(w.text);

And said that the object they were downloading was “4 mb”, that’s going to take a lot of time.