Non-blocking RPCs

Non-blocking RPCs are available in Thallium using the async() method of the callable_remote_procedure objects. In this example, we take again the sum server and show how to use the async() function on the client.

Server

As a reminder, here is the server code.

#include <iostream>
#include <thallium.hpp>

namespace tl = thallium;

void sum(const tl::request& req, int x, int y) {
    std::cout << "Computing " << x << "+" << y << std::endl;
    req.respond(x+y);
}

int main(int argc, char** argv) {

    tl::engine myEngine("tcp://127.0.0.1:1234", THALLIUM_SERVER_MODE);
    std::cout << "Server running at address " << myEngine.self() << std::endl;
    myEngine.define("sum", sum);

    return 0;
}

Client

Let’s now take a look at the client code.

#include <iostream>
#include <thallium.hpp>

namespace tl = thallium;

int main(int argc, char** argv) {
    if(argc != 2) {
        std::cerr << "Usage: " << argv[0] << " <address>" << std::endl;
        exit(0);
    }
    tl::engine myEngine("tcp", THALLIUM_CLIENT_MODE);
    tl::remote_procedure sum = myEngine.define("sum");
    tl::endpoint server = myEngine.lookup(argv[1]);
    auto request = sum.on(server).async(42,63);
    // do something else ...
    // check if request completed
    bool completed =  request.received();
    // ...
    // actually wait on the request and get the result out of it
    int ret = request.wait();
    std::cout << "Server answered " << ret << std::endl;

    return 0;
}

Timeout

There is an equivalent timed_async function that allows one to specify a timeout value in the form of an std::chrono::duration object. If the RPC times out, wait() on the resulting async_response object will throw a timeout exception.