title: How do I deal with Lua libraries that don't coroutine.yield()?
body: <p>I want to download a large file and concurrently handle other things.</p>
  
  <p>However, <a href="http://luasocket.luaforge.net/" rel="noreferrer"><code>luasocket.http</code></a> never calls <code>coroutine.yield()</code>. Everything else freezes while the file downloads.</p>
  
  <p>Here's an illustrative example, in which I try to simultaneously download a file and print some numbers:</p>
  
  <pre><code>local http = require'socket.http'
  
  local downloadRoutine = coroutine.create(function ()
      print 'Downloading large file'
      -- Download an example file
      local url = 'http://ipv4.download.thinkbroadband.com/5MB.zip'
      local result, status = http.request(url)
      print('FINISHED download ('..status..', '..#result..'bytes)')
  end)
  
  local printRoutine = coroutine.create(function ()
      -- Print some numbers
      for i=1,10 do
          print(i)
          coroutine.yield()
      end
      print 'FINISHED printing numbers'
  end)
  
  repeat
      local printActive = coroutine.resume(printRoutine)
      local downloadActive = coroutine.resume(downloadRoutine)
  until not downloadActive and not printActive
  print 'Both done!'
  </code></pre>
  
  <p>Running it produces this:</p>
  
  <pre><code>1
  Downloading large file
  FINISHED download (200, 5242880bytes)
  2
  3
  4
  5
  6
  7
  8
  9
  10
  FINISHED printing numbers
  Both done!
  </code></pre>
  
  <p>As you can see, <code>printRoutine</code> is <code>resume</code>d first. It prints the number 1 and <code>yield</code>s. The <code>downloadRoutine</code> is then <code>resume</code>d, which downloads <em>the entire file, without yielding</em>. Only then are the rest of the numbers printed.</p>
  
  <p>I don't want to write my own socket library! What can I do?</p>
  
  <p><strong>Edit (later the same day)</strong>: Some MUSH users <a href="http://www.gammon.com.au/forum/?id=11344" rel="noreferrer">have also noticed</a>. They provide helpful ideas. </p>