When is uv.run() required?
Hello,
I am new to lua and luv and am writing an nginx application that will accept large file uploads from many concurrent connections simultaneously, but I need to do some computation over the data as it is written to disk (think a checksum). I have experience using libuv from a different C project, so I thought to use it here since it would provide non-blocking, fast IO. I am a bit confused about how to execute the uv.run() loop. The default mode will block while the event loop is processed, there doesn't appear to be a way to hook ngx's event loop to also watch the libuv socket to emit events, and while I could do something like
co = coroutine.spawn(function ()
while not ngx.worker.is_exiting() do
luv.run("once")
coroutine.yield()
end
end)
I'm not sure of how to tell nginx to continue it at the appropriate times.
The thing that's mostly confusing me that I'm struggling to understand is if uv.run() is required at all. If I look at the filesystem examples in https://github.com/luvit/luv/blob/master/docs.md#file-system-operations, they don't call the event loop and appear to behave properly. Does this mean there's an implicit event loop happening somewhere behind my back?
Thanks for your help!
Running the libuv loop periodically (say, on an interval) in nowait mode is pretty much the best way to have another event loop "drive" the libuv event loop.
The synchronous fs examples work without an event loop because without a callback libuv does the IO work on the thread that initiated the fs request (ie. it blocks until IO is complete) and no event loop is necessary. The asynchronous fs examples do require an event loop because the callback is deferred until the request is completed.