Re: [Vala] Need help in async methods



It's not possible block until an async method completes, because that
would block the method progress and defeat the initial purpose.

In particular, synchronous APIs are fully synchronous (unless threads
or inner loop are involved).

You can use the 'yield' keyword to call async methods synchronously:

    public async void do_something ();
    public async void do_something_2 ();

    public async void do_everything () {
        // everything is 'sequential' here
        yield do_something ();
        yield do_something_2 ();
    }

    public void main (string[] args) {
        do_everything ();
        new MainLoop ().run ();
        return 0;
    }

Le samedi 16 janvier 2016 à 15:56 +0100, marcin saepia net a écrit :
Hello,

I am trying to create a function is synchronous and block for the
outer
world, but internally

a) does nothing unless certain callback is called
b) does not block mainloop

Something like synchronous HTTP calls in libsoup - you do request,
the
execution if caller is stopped until it's done, the result is just
returned
but internally it does asynchronous operations and does not block the
whole
program.


I read tutorial and samples but I still struggle to find the valid
pattern,
I came to the point that looks more less like this:

public class MyClass : Object {
  private OtherClass other;
  private SourceFunc other_callback;
  private bool       other_result;


  public bool dosomething() {
    // What should I put here to block callee and call
dosomething_async
asynchronously?
  }


  public async bool dosomething_async() {
    this.other = new OtherClass();
    this.other.finished.connect(on_other_finished);

    this.other_callback = dosomething_async.callback;
    yield;
    // Now everything except the callee should continue execution
    // unless OtherClass sends us a callback
  }
}


private void on_other_finished(bool result) {
  // Task is finished, should unblock dosomething();
  this.other_result = result;
  this.other_callback();
}


int main(string[] argv) {
  var instance = new MyClass();
  var result = instance.dosomething(); // <-- this should block

  return 0;
}


Thank you for your help

m.
_______________________________________________
vala-list mailing list
vala-list gnome org
https://mail.gnome.org/mailman/listinfo/vala-list
-- 
Guillaume Poirier-Morency <guillaumepoiriermorency gmail com>

Étudiant au baccalauréat en Informatique à l'Université de Montréal
Développeur d'application web

Mon blog: https://arteymix.github.io/
Mon projet de coopérative: https://pittoresque.github.io/
Clé PGP: B1AD6EA5

Attachment: signature.asc
Description: This is a digitally signed message part



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