Re: [Vala] [Valum]help with process async method.



Le mardi 24 novembre 2015 à 20:59 +0800, Matrix a écrit :
Hi:

i'm writing a server side program with valum framework...

in one entry point,  the server will send a http request to a remote 
server,

You will need libsoup-2.4 for that, here's an example: https://github.c
om/valum-framework/valum/blob/master/examples/api-interaction/app.vala


i copy the https://wiki.gnome.org/Projects/Vala/AsyncSamples in my
code 
to test


async double do_calc_in_bg(double val) throws ThreadError {
    SourceFunc callback = do_calc_in_bg.callback;
    double[] output = new double[1];

    // Hold reference to closure to keep it from being freed whilst
    // thread is active.
    ThreadFunc<void*> run = () => {
        // Perform a dummy slow calculation.
        // (Insert real-life time-consuming algorithm here.)
        double result = 0;
        for (int a = 0; a<10000000; a++)
            result += val * a;

        // Pass back result and schedule callback
        output[0] = result;
        Idle.add((owned) callback);
        return null;
    };
    Thread.create<void*>(run, false);

    // Wait for background thread to schedule our callback
    yield;
    return output[0];
}

void test(Request request, Response response) {

      do_calc_in_bg.begin(0.001, (obj, res) => {
              try {
                      double result = do_calc_in_bg.end(res);
                      response.write_all(@"yes, the result is
$result".data, null);
              } catch (ThreadError e) {
                      string msg = e.message;
                      stderr.printf(@"Thread error: $msg\n");
              }
      });
}


but when the thread finished,  call the response.write_all, it fails
...

2015-11-24 20:37:32: (mod_fastcgi.c.2391) unexpected end-of-file 
(perhaps the fastcgi process died): pid: 6667 socket: 
unix:/opt/projects/platformX/install/valum.sock-0
2015-11-24 20:37:32: (mod_fastcgi.c.3172) response not received, 
request sent: 548 on socket: 
unix:/opt/projects/platformX/install/valum.sock-0 for 
/verify_code?phone=13300000000, closing connection

FastCGI relies on a blocking function call (namely 'accept') that is
triggered on incoming data. If you decide to delegate some processing
to the GMainLoop, it is likely to be called and block any form of
progress until a new request is received.

I have attempted to solve this issue by the past, but nothing
successful arose.

I think I can do something like:

 - accept in a thread
 - delegate further processing to the GMainLoop

It's important that actual request processing happen in the main
thread, because threading should be a choice left to the developer.

I am also working on wrapping fcgi.vapi in a more GIO-friendly way.

I suggest that you use SCGI for now if you plan to make use of
asynchronous programming, otherwise you can use the synchronous version
of 'send' http://valadoc.org/#!api=libsoup-2.4/Soup.Session.send


how to implement async method ?

3ks a lot!!!

Cheers!




_______________________________________________
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]