Re: [Vala] Combining async methods with external processes



On Fri, Nov 19, 2010 at 13:25:11 +0100, pancake wrote:
you are using coroutines which are not designed to "run stuff in
background".

coroutines are designed to run collaborative code which hangs if one of the
parts doesn't pass the token when necessary.

You have to use threads, or an idle task if you have a mainloop.

Async methods are NOT coroutines(*). They ARE transformation of functions to
let them finish in response to an event in the main loop (idle or not).

Oh, and avoid threads as long as you can -- synchronization is a LOT of
trouble. And running another main loop in the other thread to get
spawn_async_with_pipes working there is even more.

On 11/19/10 08:34, James Moschou wrote:
Hello,

I want to perform a series of tasks one after the other, but in the
background. So I have:

async bool perform_tasks () {
    bool result = yield task1 ();
    if (result)
        result = yield task2 ();
    if (result)
        result = yield task3 ();

    Idle.add (perform_tasks.callback)

You can attach the .callback to any event source in the same way. So e.g.
when data arrive from the process or the child terminates etc.

    yield;
    return result;
}

more or less.

One of the tasks however spawns a new process, and I'm not sure how
the callback from this process is supposed to fit into Vala's way of
doing async programming, i.e. Process.spawn_async_with_pipes () isn't
actually an "async" method.

But it's easy to wrap it in one. Just spawn_async_with_pipes and arrange the
.callback to be called when results are available and yield.


(*) They are not coroutines, because they are not running in parallel with
the caller. The not yet implemented generator functions would be coroutines.

-- 
                                                 Jan 'Bulb' Hudec <bulb ucw cz>



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]