How to use Gnome GIO to read a file by chunks in non-blocking way?

江枫思渺然 提交于 2019-12-12 01:42:41

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!