我想执行异步文件IO操作。这是我在 Vala 中尝试做的事情的简化版本:
void main(string[] args) {
store_async();
while(true)
;
}
async void store_async() {
File file = File.new_for_path("settings.ini");
stderr.printf("Checking if file exists...\n");
if (!file.query_exists()) {
stderr.printf("About to yield file.create_async...\n");
try {
yield file.create_async(FileCreateFlags.REPLACE_DESTINATION);
} catch (Error err) {
error("Error creating file: %s\n", err.message);
}
stderr.printf("Returned from file.create_async.\n");
}
string data = "hello\n";
string new_etag;
stderr.printf("About to yield file.replace_contents_async...\n");
try {
yield file.replace_contents_async(
data.data,
null,
false,
FileCreateFlags.NONE,
null,
out new_etag);
} catch (Error err) {
error("Error replacing contents: %s\n", err.message);
}
stderr.printf("Returned from file.replace_contents_async.\n");
}
当我运行此程序时,如果没有文件 settings.ini
存在,则会创建 settings.ini
并且我看到以下输出:
Checking if file exists...
About to yield file.create_async...
(hangs)
如果 settings.ini
存在,则不会向其中写入任何内容,并且我会看到以下输出:
Checking if file exists...
About to yield file.create_async...
(hangs)
如果我尝试在 C 中重现该问题,我也会遇到类似的问题。这是我的 C 代码(它不会复制上面的整个 vala 示例,仅复制创建文件的部分):
#include <glib.h>
#include <gio/gio.h>
#include <stdio.h>
void create_callback(GObject *source_object, GAsyncResult *res, gpointer user_data);
void write_contents();
GFile* file;
void main(int argc, char** argv) {
g_type_init();
fprintf(stderr, "Before file_new_for_path\n");
file = g_file_new_for_path("settings.ini");
fprintf(stderr, "Before file_query_exists\n");
if (!g_file_query_exists(file, NULL)) {
fprintf(stderr, "Before file_create_async\n");
g_file_create_async(
file,
G_FILE_CREATE_REPLACE_DESTINATION,
G_PRIORITY_DEFAULT,
NULL,
create_callback,
NULL);
fprintf(stderr, "After file_create_async\n");
} else {
fprintf(stderr, "File already exists. Before write_contents\n");
write_contents();
fprintf(stderr, "File already exists. After write_contents\n");
}
while(TRUE)
;
}
void create_callback(GObject *source_object, GAsyncResult *res, gpointer user_data) {
fprintf(stderr, "In create_callback. Before write_contents.\n");
write_contents();
fprintf(stderr, "In create_callback. After write_contents.\n");
}
void write_contents() {
fprintf(stderr, "In write_contents\n");
}
当我运行此示例时,我看到以下输出(假设 settings.ini
不存在):
Before file_new_for_path
Before file_query_exists
Before file_create_async
After file_create_async
(hangs)
换句话说,create_callback
永远不会被调用。
我做错了什么?为什么当我调用 g_file_create_async
和 g_file_replace_contents_async
时它们没有完成?
最佳答案
glib 中的异步实现要求您运行消息循环。如果没有,您的回调将如何以及何时被调用?
所以在 Vala 中只需添加:
var loop = new MainLoop();
loop.run();
在 C 中:
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
g_main_loop_unref(loop)
关于c - 在GIO中,为什么这些异步文件IO操作永远不会完成? (适用于 C 和 Vala),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10791196/