问题
What is the right (GIO/Glib/GTK/Gnome) way to process GInputStream in non-blocking manner and chunk-by-chunk?
I have an application which is downloading (through libsoup) and processing a data stream in chunks and doing other actions in parallel. I am calling g_input_stream_read_async
on GInputStream (received from soup_session_send_finish
and giving it a reasonable size of chunk to read (in my case 2048 bytes).
After I receive a g_input_stream_read_async
callback, I want to continue reading bytes. So, the first idea is to recursively call g_input_stream_read_async
from the callback handler, passing itself as next callback. But that seems clumsy and not quite right to me (and I'm not sure if it's safe to pass to GIO the callback which is currently still executing).
Alternative could be to spin off a thread and do usual blocking reads in a loop calling g_input_stream_read
.
But how it is usually done in GTK / Gnome world? What is the right way? Any simple working example (preferably from developers related to GTK / Gnome) will be appreciated.
回答1:
After I receive a g_input_stream_read_async callback, I want to continue reading bytes. So, the first idea is to recursively call g_input_stream_read_async from the callback handler, passing itself as next callback.
This is valid and works well.
来源:https://stackoverflow.com/questions/39184621/how-to-use-gnome-gio-to-read-a-file-by-chunks-in-non-blocking-way