Async called procedures

45 views
Skip to first unread message

Greg Miller

unread,
Oct 28, 2014, 4:42:46 PM10/28/14
to autob...@googlegroups.com
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?

Tobias Oberstein

unread,
Oct 28, 2014, 5:58:13 PM10/28/14
to autob...@googlegroups.com
Am 28.10.2014 21:42, schrieb Greg Miller:
> 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;)

>
> --
> 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.

Greg Miller

unread,
Oct 28, 2014, 6:09:31 PM10/28/14
to autob...@googlegroups.com
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?

redabo...@gmail.com

unread,
Oct 28, 2014, 6:18:25 PM10/28/14
to autob...@googlegroups.com
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;
};

Tobias Oberstein

unread,
Oct 28, 2014, 6:18:52 PM10/28/14
to autob...@googlegroups.com
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+...@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>.

Greg Miller

unread,
Oct 28, 2014, 6:28:59 PM10/28/14
to autob...@googlegroups.com
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.

redabo...@gmail.com

unread,
Oct 28, 2014, 7:29:47 PM10/28/14
to autob...@googlegroups.com
Thank you for this reply Tobias. Your code example has caused a moment of epiphany for me!
Reply all
Reply to author
Forward
0 new messages