luvit / luv

Bare libuv bindings for lua
Apache License 2.0
832 stars 187 forks source link

Do I have to use `uv.run()` to wait async reading file with `fs_read` API? #675

Closed linrongbin16 closed 1 year ago

linrongbin16 commented 1 year ago

Hi, I'm reading the file operation in the doc: https://github.com/luvit/luv/blob/master/docs.md#file-system-operations, I feel like I understand the sync and async operation.

While I want to make sure about one thing, in async fs_read, usually we registered a callback function, which is called when the file read operating is done.

But when fs_read is doing the job in another thread (I'm not quite sure if this is the correct words to describe it, I just mean that while fs_read is reading the file, I can still do something in the main thread), what should I do to wait the fs_read until it's done? use the uv.run() API just like when I use the uv.spawn() ?

linrongbin16 commented 1 year ago

Or the very inside callback uv.fs_close in uv.fs_read will block all the callback chain ?

And 1 more question, since I want to write something lower the latency of reading file, I'm thinking that I should use offset and size to read block by block (for example every block 4096 bytes). Since there're so many callbacks, I think I can use lua coroutine to yield to a consumer function to consume the readed data, and this fs_read is a producer function to produce data.

linrongbin16 commented 1 year ago

I have tried the async sample in doc: https://github.com/luvit/luv/blob/master/docs.md#file-system-operations, but it looks like it's not going to wait until the fs_read complete, so now I'm using the sync fs_read with a batchsize=4096 to batch read the file.