In GIO, why do these asynchronous file IO operations never complete? (applies to both C and Vala)

StackOverflow https://stackoverflow.com/questions/10791196

Pregunta

I want to perform asynchronous file IO operations. Here is a simplified version of what I am trying to do in 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");
}

When I run this program, and no file settings.ini exists, settings.ini is created and I see this output:

Checking if file exists...
About to yield file.create_async...
(hangs)

If settings.ini exists, nothing is written to it, and I see this output:

Checking if file exists...
About to yield file.create_async...
(hangs)

I have a similar problem if I try to reproduce the issue in C. Here is my C code (it doesn't replicate the entire vala example above, just the part that creates the file):

#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");
}

When I run this example, I see this output (assuming settings.ini does not exist):

Before file_new_for_path
Before file_query_exists
Before file_create_async
After file_create_async
(hangs)

In other words, create_callback is never called.

What am I doing wrong? Why don't g_file_create_async and g_file_replace_contents_async ever complete when I call them?

¿Fue útil?

Solución

The async implementations in glib require that you run a message loop. If not, how and when would your callbacks be called?

So in Vala just add:

var loop = new MainLoop();
loop.run();

And in C:

GMainLoop *loop = g_main_loop_new(NULL, FALSE);
g_main_loop_run(loop);
g_main_loop_unref(loop)
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top