Re: [Vala] [Async] why use .begin ?
On Sat, Sep 19, 2009 at 01:41:34 +0200, Michael 'Mickey' Lauer wrote: No, the call without .begin() is being deprecated. From yestarday's discussion on IRC I understood it's not generally possible to collapse to synchronous call automagically. Doing it involves running a (recursive) main loop until the callback is called, but this brings a lot of problems like something quitting the outer loop, the nested loops being quit out of order and such. Therefore it should not be done without user's explicit request. I see, am I correct then in assuming that at the end of the day this means, calling an asynchronous function with out/return arguments is forbidden/will not work from synchronous code? It is allowed and works perfectly fine. Just you have to do it the manual way -- pass a callback in and collect the return value there. It's four lines like: Whatever result; var loop = new MainLoop(); foo_async.begin((r) = { result = foo_async.finish(); loop.quit(); } loop.run(); // do whatever you want with result here. So it's doable, but a bit of typing and remembering how the MainLoop is run. I can imagine a special syntax that will make vala generate this might be accepted in future. My suggestion would be var result = yield sync foo_async(); This would make sync special only after yield and allow featuring a large warning in the documentation about the pitfalls you may fall into if you actually call it from a main loop (while this is normally used for modal dialogs, they avoid most of the traps by blocking events from other windows). (Didier, please note that if that's true, then it's a strong argument in favor of letting vala-dbus-binding-tool emit both a sync. and an async. dbus vala interface, since the async. interface is then basically useless to call from synchronous code). Yes, it should. Especially if it has a method to avoid the traps (running a separate MainContext?). -- Jan 'Bulb' Hudec b...@ucw.cz ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
On Sat, Sep 19, 2009 at 15:40:16 +0200, Didier Ptitjes wrote: Jan Hudec wrote: I can imagine a special syntax that will make vala generate this might be accepted in future. My suggestion would be var result = yield sync foo_async(); I guess I would propose: var result = foo_async(); We talked about that on IRC today with Jürg, and it may be possible to implement such a thing, correctly with g_main_context_push_thread_default() (GLib 2.21). I didn't notice that thing -- it's indeed good tool for the job. I would still prefer something to mark in the source that we are synchronizing an async function. Maybe a .sync() submethod to match .begin() and .end() and state clearly what is happening: var result = foo_async.sync(...); var result = foo_async.synchronously(...); Whenever the main loop is running, using async functions is prefered in most cases, so we don't want people using the synchronous variant by mistake and than complain that their application is unresponsive. Besides I am not sure it's completely idiotproof -- if you wrote an async function that did something with the gui and than called it synchronized, it could fail spectacularly or behave weirdly, because the it's run in different main context than the gui (and the gui one is not running). -- Jan 'Bulb' Hudec b...@ucw.cz ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
Jan Hudec wrote: I didn't notice that thing -- it's indeed good tool for the job. I would still prefer something to mark in the source that we are synchronizing an async function. Maybe a .sync() submethod to match .begin() and .end() and state clearly what is happening: .sync() is exactly what I proposed Jürg initially :p ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
On Friday 18 September 2009 18:18:33 JM wrote: Hi all A few days ago I edited the asynchronous stream reading example on the vala site to use the new async syntax. http://live.gnome.org/Vala/GIOSamples#head-a6170c01121b9fe5825f431de73573b0 84545741 After the release of vala-0.7.6 I realized that other examples of the new syntax use '.begin()' to call the async function. I compiled the example with and without the .begin keyword and found NO DIFFERENCE in the generated C code. Both ways are working properly. Is there a case where the difference will be more clear? Right now I think they're equal, although my view is that it should make a difference when calling an async function from a synchronous one. The principle of least surprise should be that begin() and end() would be collapsed to one synchronous call in that particular case. Right now async has a related problem though for async methods that return values, i.e. the following code: async int func() { return 10; } void main() { int i = func(); } leads to the -- somewhat surprising -- error message: mic...@andromeda:/local/pkg/vala/test$ valac --pkg gio-2.0 foo.vala foo.vala:8.10-8.15: error: invocation of void method not allowed as expression int i = func(); ^^ since internally the invocation is moved to func.begin(), which is (always) a void method. This is no problem from async to async as we would be using int i = yield func() in that case, however it's quite common to call async() from sync(), hence some additional work is required here. :M: :M: ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
Ok. Thanks! PS.: Can someone who knows the async stuff better than me write a few lines about .begin and .end to the vala tutorial? Regards, Jörn Am Freitag, den 18.09.2009, 18:34 +0200 schrieb Michael 'Mickey' Lauer: On Friday 18 September 2009 18:18:33 JM wrote: Hi all A few days ago I edited the asynchronous stream reading example on the vala site to use the new async syntax. http://live.gnome.org/Vala/GIOSamples#head-a6170c01121b9fe5825f431de73573b0 84545741 After the release of vala-0.7.6 I realized that other examples of the new syntax use '.begin()' to call the async function. I compiled the example with and without the .begin keyword and found NO DIFFERENCE in the generated C code. Both ways are working properly. Is there a case where the difference will be more clear? Right now I think they're equal, although my view is that it should make a difference when calling an async function from a synchronous one. The principle of least surprise should be that begin() and end() would be collapsed to one synchronous call in that particular case. Right now async has a related problem though for async methods that return values, i.e. the following code: async int func() { return 10; } void main() { int i = func(); } leads to the -- somewhat surprising -- error message: mic...@andromeda:/local/pkg/vala/test$ valac --pkg gio-2.0 foo.vala foo.vala:8.10-8.15: error: invocation of void method not allowed as expression int i = func(); ^^ since internally the invocation is moved to func.begin(), which is (always) a void method. This is no problem from async to async as we would be using int i = yield func() in that case, however it's quite common to call async() from sync(), hence some additional work is required here. :M: :M: ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
On Fri, Sep 18, 2009 at 18:34:47 +0200, Michael 'Mickey' Lauer wrote: On Friday 18 September 2009 18:18:33 JM wrote: Hi all A few days ago I edited the asynchronous stream reading example on the vala site to use the new async syntax. http://live.gnome.org/Vala/GIOSamples#head-a6170c01121b9fe5825f431de73573b0 84545741 After the release of vala-0.7.6 I realized that other examples of the new syntax use '.begin()' to call the async function. I compiled the example with and without the .begin keyword and found NO DIFFERENCE in the generated C code. Both ways are working properly. Is there a case where the difference will be more clear? Right now I think they're equal, although my view is that it should make a difference when calling an async function from a synchronous one. The principle of least surprise should be that begin() and end() would be collapsed to one synchronous call in that particular case. No, the call without .begin() is being deprecated. From yestarday's discussion on IRC I understood it's not generally possible to collapse to synchronous call automagically. Doing it involves running a (recursive) main loop until the callback is called, but this brings a lot of problems like something quitting the outer loop, the nested loops being quit out of order and such. Therefore it should not be done without user's explicit request. The reason for .begin() is mostly a symetry with .end() and code readability where the use of .begin() clearly indicates that asynchronous operation is being started (and will continue beyond the statement). Right now async has a related problem though for async methods that return values, i.e. the following code: async int func() { return 10; } void main() { int i = func(); } leads to the -- somewhat surprising -- error message: mic...@andromeda:/local/pkg/vala/test$ valac --pkg gio-2.0 foo.vala foo.vala:8.10-8.15: error: invocation of void method not allowed as expression int i = func(); ^^ since internally the invocation is moved to func.begin(), which is (always) a void method. This is no problem from async to async as we would be using int i = yield func() in that case, however it's quite common to call async() from sync(), hence some additional work is required here. There are two ways to call asynchronous method from synchronous one. You may want to just start the operation and register a callback for it or you may want to wait in a new instance of main loop until the operation completes. The bare func() would be somewhat ambiguous in this respect and so it's being deprecated. There may eventually be some construct to call asynchronous operation synchronously, but it is unlikely to be a simple call. -- Jan 'Bulb' Hudec b...@ucw.cz ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
Hello What does .end() do? Are there any examples available? Please also put some information to the tutorial. That would be great! Thanks Jörn No, the call without .begin() is being deprecated. From yestarday's discussion on IRC I understood it's not generally possible to collapse to synchronous call automagically. Doing it involves running a (recursive) main loop until the callback is called, but this brings a lot of problems like something quitting the outer loop, the nested loops being quit out of order and such. Therefore it should not be done without user's explicit request. The reason for .begin() is mostly a symetry with .end() and code readability where the use of .begin() clearly indicates that asynchronous operation is being started (and will continue beyond the statement). Right now async has a related problem though for async methods that return values, i.e. the following code: async int func() { return 10; } void main() { int i = func(); } leads to the -- somewhat surprising -- error message: mic...@andromeda:/local/pkg/vala/test$ valac --pkg gio-2.0 foo.vala foo.vala:8.10-8.15: error: invocation of void method not allowed as expression int i = func(); ^^ since internally the invocation is moved to func.begin(), which is (always) a void method. This is no problem from async to async as we would be using int i = yield func() in that case, however it's quite common to call async() from sync(), hence some additional work is required here. There are two ways to call asynchronous method from synchronous one. You may want to just start the operation and register a callback for it or you may want to wait in a new instance of main loop until the operation completes. The bare func() would be somewhat ambiguous in this respect and so it's being deprecated. There may eventually be some construct to call asynchronous operation synchronously, but it is unlikely to be a simple call. ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
On Fri, Sep 18, 2009 at 19:52:59 +0200, JM wrote: What does .end() do? Are there any examples available? It's a syntax for the _finish function. Takes the AsyncResult and return the real result or throws an error (if the async function does). It's called like async_function.end(result) in the async callback. -- Jan 'Bulb' Hudec b...@ucw.cz ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
Ah. ok. Can you also say something about async_function.callback I've seen it in the dbus test, but I don't fully understand it yet. Regards Jörn Am Freitag, den 18.09.2009, 20:10 +0200 schrieb Jan Hudec: On Fri, Sep 18, 2009 at 19:52:59 +0200, JM wrote: What does .end() do? Are there any examples available? It's a syntax for the _finish function. Takes the AsyncResult and return the real result or throws an error (if the async function does). It's called like async_function.end(result) in the async callback. ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
No, the call without .begin() is being deprecated. From yestarday's discussion on IRC I understood it's not generally possible to collapse to synchronous call automagically. Doing it involves running a (recursive) main loop until the callback is called, but this brings a lot of problems like something quitting the outer loop, the nested loops being quit out of order and such. Therefore it should not be done without user's explicit request. I see, am I correct then in assuming that at the end of the day this means, calling an asynchronous function with out/return arguments is forbidden/will not work from synchronous code? (Didier, please note that if that's true, then it's a strong argument in favor of letting vala-dbus-binding-tool emit both a sync. and an async. dbus vala interface, since the async. interface is then basically useless to call from synchronous code). Cheers, :M: ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list
Re: [Vala] [Async] why use .begin ?
On Sat, 2009-09-19 at 01:41 +0200, Michael 'Mickey' Lauer wrote: No, the call without .begin() is being deprecated. From yestarday's discussion on IRC I understood it's not generally possible to collapse to synchronous call automagically. Doing it involves running a (recursive) main loop until the callback is called, but this brings a lot of problems like something quitting the outer loop, the nested loops being quit out of order and such. Therefore it should not be done without user's explicit request. I see, am I correct then in assuming that at the end of the day this means, calling an asynchronous function with out/return arguments is forbidden/will not work from synchronous code? You can certainly call an asynchronous method with out/return values, however, you obviously have to wait for the method to return until you can use them. You could, for example, handle them in a closure used as callback to .begin. Jürg ___ Vala-list mailing list Vala-list@gnome.org http://mail.gnome.org/mailman/listinfo/vala-list