adding callbacks and set_expiry on async objects

538 views
Skip to first unread message

Mark

unread,
May 13, 2013, 3:23:28 AM5/13/13
to rp...@googlegroups.com
Hi everyone,

I am trying to setup a method using async objects, and I am having a number of issues including the use of storing callbacks in a dictionary for future use.  Also, the async objects seem to call the callbacks prior to receiving the data, or before a timeout is reached.  Sample code below:

# trying to call an async object
rc = rpyc.connect('', port=10000)
exposed_method = rpyc.async(rc.root.exposed_method)
asyncFunc = exposed_method("test data","test data 2")
asyncFunc.set_expiry(10)
asyncFunc.add_callback(self.relay_callback("some value"))
self.asyncFuncDict[key] = asyncFunc
self.callbacks[key] = client_callback

# in my local callback function (relay_callback)  
asyncFunc = self.asyncFuncDict.get(key)
client_callback = self.callbacks.get(key)
asyncFunc.wait()
client_callback(asyncFunc.value)

So, I am trying to call another process first by connecting to it, and then setting up an async wrapper around a method exposed by the other process.  I want my relay_callback function to be called when either the value is ready, or the timeout has been reached.  I try to save a handle to my asyncFunc and my client_callback so that I can use them later to relay the value.  However, when I pull the client_callback out of my dictionary, I am unable to use it as a callback.  Python thinks it is just a string.  Also, my "relay_callback" function is called before the value is ready or the timeout is expired, which is not what I would expect. I am unable to call wait() (or any other methods of an AsyncResult object) on my asyncFunc, since it is no longer recognized as an AsyncResult.

Thank you for any insight you can provide.

-Mark

Tomer Filiba

unread,
May 13, 2013, 5:44:46 AM5/13/13
to rpyc
I'm not sure I understand your flow. Here's a canonical example:

>>> c=rpyc.classic.connect("localhost")
>>> c.execute("""def f(a,b):
...     import time
...     time.sleep(8)
...     return a+b""")
>>> f = c.namespace["f"]
>>> af = rpyc.async(f)
>>>
>>>
>>> def cb(res):
...     print "I am ready", res
...
>>> ar=af(1,2);ar.add_callback(cb)
>>> ar.wait()                                         # takes 8 seconds...
I am ready <AsyncResult object (ready) at 0x02889d20> # printed by the callback
>>> ar.value
3

If you set an expiry, wait() will raise an AsyncResultTimeout, and the callback would not be invoked:
>>> ar=af(1,2);ar.set_expiry(3);ar.add_callback(cb);ar.wait()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rpyc\core\async.py", line 58, in wait
    raise AsyncResultTimeout("result expired")
rpyc.core.async.AsyncResultTimeout: result expired
>>> ar.value
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "rpyc\core\async.py", line 113, in value
    self.wait()
  File "rpyc\core\async.py", line 58, in wait
    raise AsyncResultTimeout("result expired")
rpyc.core.async.AsyncResultTimeout: result expired



-tomer


-----------------------------------------------------------------
    
Tomer Filiba 
tomerfiliba.com        



-Mark

--
 
---
You received this message because you are subscribed to the Google Groups "rpyc" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rpyc+uns...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Reply all
Reply to author
Forward
0 new messages