callback example

22 views
Skip to first unread message

John Roll

unread,
Aug 7, 2014, 12:57:10 PM8/7/14
to gevent-...@googlegroups.com

Hi,

Are the any examples of using Basenamespace.emit(..., callback=some_python_def)?

Is there an easy way to make a synchronous call to the client and get a return value?

Thanks,

John

John Roll

unread,
Aug 11, 2014, 11:04:15 AM8/11/14
to gevent-...@googlegroups.com
MsgResults = {}         # Global hash of outstanding messages.
MsgID      = 0          # Next msgid to use


def NextID():
    global MsgID

    MsgID = MsgID + 2

    return MsgID

def AsyncMsgHandler(reply):
    MsgResults[reply["msgid"]].set(reply["value"])


class API(object):

    def _AsyncMsgEmit(self, msg):
        msgid = NextID()

        MsgResults[msgid] = AsyncResult();
        msg["msgid"]      = msgid

        self.emit("msg", msg, callback=AsyncMsgHandler)

        print "Waiting"
        MsgResults[msgid].wait()
        print "Waited",  MsgResults[msgid].value

        value = MsgResults[msgid].value
        del MsgResults[msgid]

        return value;
Reply all
Reply to author
Forward
0 new messages