Async called procedures

#1

Can the called procedure be async? For example, making a DB request.
My guess is no. But if I’m wrong what would be the correct way to setup the called function?
If no, then I’m assuming we need to use Fibers to make it sync?

0 Likes

#2

Can the called procedure be async? For example, making a DB request

Sure, no problem.

Procedures that are exposed via WAMP (that is, procedures REGISTERed for remote calling) can do anything, including asynch stuff like DB access, doing another (outgoing) async WAMP call, doing a plain asynch Ajax thing, ...

My guess is no. But if I'm wrong what would be the correct way to setup
the called function?

Not sure what you are asking here, since e.g. in AutobahnPython there is nothing to be done to allow a procedure to do asynch stuff inside - just do it.

If you are asking specifically about doing asynch _DB_ stuff, that depends on the language and DB you want to know about.

E.g. here is Python/Twisted/PostgreSQL:

https://github.com/crossbario/crossbar/wiki/Database-Programming-with-PostgreSQL

If no, then I'm assuming we need to use Fibers to make it sync?

Noooo. No threads. No fibers. Asynchronous is more fun - most of the time;)

···

Am 28.10.2014 21:42, schrieb Greg Miller:

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/65341886-918b-4fc8-8e15-a23946e99dc0%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/65341886-918b-4fc8-8e15-a23946e99dc0%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#3

I agree that async all around is much better. I’ll clarify what I mean.
On the node.js server: (I omitted error trapping)

function getData(args) {
db.select(args, function(err, res) {
// waiting on results
return res;
})

// function implicitly returns here.

}

session.register(‘db.select’, getData)

With the above, when getData is called from the caller it will return before the db.select has completed.
Can an RPC called rtn return a promise?

0 Likes

#4

I am wondering the same. I am currently having to do the following as a means to get my results back to the client:

Core.prototype.requestGuestAccount = function(args, kwargs, _this){
_this.rclient.incr(‘base:guestDispenserCount’, function(error, reply){
_this.session.publish(‘com.base.dispenseGuestAccount’,[reply],{}, {eligible: [kwargs.sessionid]});
});
return 1;
};

···

On Tuesday, 28 October 2014 22:09:31 UTC, Greg Miller wrote:

I agree that async all around is much better. I’ll clarify what I mean.
On the node.js server: (I omitted error trapping)

function getData(args) {
db.select(args, function(err, res) {
// waiting on results
return res;
})

// function implicitly returns here.

}

session.register(‘db.select’, getData)

With the above, when getData is called from the caller it will return before the db.select has completed.
Can an RPC called rtn return a promise?

0 Likes

#5

I agree that async all around is much better. I'll clarify what I mean.
On the node.js server: (I omitted error trapping)

function getData(args) {
    db.select(args, function(err, res) {
         // waiting on results
         return res;
     })

     // function implicitly returns here.
}

session.register('db.select', getData)

With the above, when getData is called from the caller it will return
before the db.select has completed.
Can an RPC called rtn return a promise?

Of course.

AutobahnJS automatically detects if the return value from a registered procedure is "plain" or a promise and acts accordingly.

In other words, something like this (using whenjs for Deferreds - analog for other promise/deferred impl.):

function getData(args) {
    var d = when.defer();

    db.select(args, function(err, res) {
       // waiting on results
       d.resolve(res);
    })

    // whenjs has the actual user promise in an attribute
    return d.promise;
}

···

Am 28.10.2014 23:09, schrieb Greg Miller:

--
You received this message because you are subscribed to the Google
Groups "Autobahn" group.
To unsubscribe from this group and stop receiving emails from it, send
an email to autobahnws+...@googlegroups.com
<mailto:autobahnws+...@googlegroups.com>.
To post to this group, send email to autob...@googlegroups.com
<mailto:autob...@googlegroups.com>.
To view this discussion on the web visit
https://groups.google.com/d/msgid/autobahnws/6157d280-1661-43ce-ad13-088e97d9905e%40googlegroups.com
<https://groups.google.com/d/msgid/autobahnws/6157d280-1661-43ce-ad13-088e97d9905e%40googlegroups.com?utm_medium=email&utm_source=footer>.
For more options, visit https://groups.google.com/d/optout.

0 Likes

#6

Excellent! I guess in my mind the called rtn is being called by my callee, over the wire. But in reality it must be crossbar calling my routine and handling the promise. I was thinking the promise got serialized and sent back over the wire, which would be fancy.

One more mystery solved!

Thank you.

0 Likes

#7

Thank you for this reply Tobias. Your code example has caused a moment of epiphany for me!

···

On Tuesday, 28 October 2014 22:18:52 UTC, Tobias Oberstein wrote:

Am 28.10.2014 23:09, schrieb Greg Miller:

I agree that async all around is much better. I’ll clarify what I mean.

On the node.js server: (I omitted error trapping)

function getData(args) {

db.select(args, function(err, res) {
     // waiting on results
     return res;
 })
 // function implicitly returns here.

}

session.register(‘db.select’, getData)

With the above, when getData is called from the caller it will return

before the db.select has completed.

Can an RPC called rtn return a promise?

Of course.

AutobahnJS automatically detects if the return value from a registered
procedure is “plain” or a promise and acts accordingly.

In other words, something like this (using whenjs for Deferreds - analog
for other promise/deferred impl.):

function getData(args) {

var d = when.defer();



db.select(args, function(err, res) {

   // waiting on results

   d.resolve(res);

})



// whenjs has the actual user promise in an attribute

return d.promise;

}

You received this message because you are subscribed to the Google

Groups “Autobahn” group.

To unsubscribe from this group and stop receiving emails from it, send

an email to autobahnws+...@googlegroups.com

mailto:autobahnws+unsub...@googlegroups.com.

To post to this group, send email to auto...@googlegroups.com

mailto:autob...@googlegroups.com.

To view this discussion on the web visit

https://groups.google.com/d/msgid/autobahnws/6157d280-1661-43ce-ad13-088e97d9905e%40googlegroups.com

<https://groups.google.com/d/msgid/autobahnws/6157d280-1661-43ce-ad13-088e97d9905e%40googlegroups.com?utm_medium=email&utm_source=footer>.

For more options, visit https://groups.google.com/d/optout.

0 Likes