2 # this demo will show you working with asynch proxies and callback
\r
3 # verison 2.3 removes the AsyncCallback factory, and instead provides a mechanism
\r
4 # where async results can provide a callback. it simplifies the design, so i
\r
8 from Rpyc.Factories import SocketConnection, Async
\r
10 c1 = SocketConnection("localhost")
\r
12 # f1 is an async proxy to the server's sleep function
\r
13 f1 = Async(c1.modules.time.sleep)
\r
15 # this would block the server for 9 seconds
\r
17 # and this would block it for 11
\r
20 # of course the client isnt affected (that's the whole point of Async)
\r
21 # but since the same server can't block simultaneously, the second request is
\r
22 # queued. this is a good example of queuing.
\r
24 # now we'll wait for both results to finish. this should print around 20 lines
\r
25 # (more or less, depending on the phase)
\r
26 while not r1.is_ready or not r2.is_ready:
\r
32 # now we'll dig in the h4xx0r shit -- running things simultaneously
\r
33 # for this, we'll need another connection, and another proxy:
\r
34 c2 = SocketConnection("localhost")
\r
35 f2 = Async(c2.modules.time.sleep)
\r
37 # now we'll do the same as the above, but this time, it will happen simulatenously
\r
38 # becuase f1 and f2 work on different connections
\r
42 # so this time, it will print around 11 lines
\r
43 while not r1.is_ready or not r2.is_ready:
\r
49 # very haxxor indeed. now, we'll see how to use the on_ready callback
\r
54 print "look mama, no hands! res = %r" % (res.result,)
\r
56 # set the on_ready callback -- when r1 is becomes ready, the callback will
\r
57 # be called automagically
\r
60 # this should print 9 "!", then "look mama", then two more "!"
\r
61 while not r1.is_ready or not r2.is_ready:
\r