report any expired sites & nodes
[monitor.git] / Rpyc / Demo / demo-5.py
1 #\r
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
5 # went for it.\r
6 #\r
7 import time\r
8 from Rpyc.Factories import SocketConnection, Async\r
9 \r
10 c1 = SocketConnection("localhost")\r
11 \r
12 # f1 is an async proxy to the server's sleep function\r
13 f1 = Async(c1.modules.time.sleep)\r
14 \r
15 # this would block the server for 9 seconds\r
16 r1 = f1(9)\r
17 # and this would block it for 11\r
18 r2 = f1(11)\r
19 \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
23 \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
27     print "!"\r
28     time.sleep(1)\r
29 \r
30 print "---"\r
31 \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
36 \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
39 r1 = f1(9)\r
40 r2 = f2(11)\r
41 \r
42 # so this time, it will print around 11 lines\r
43 while not r1.is_ready or not r2.is_ready:\r
44     print "!"\r
45     time.sleep(1)\r
46 \r
47 print "---"\r
48 \r
49 # very haxxor indeed. now, we'll see how to use the on_ready callback\r
50 r1 = f1(9)\r
51 r2 = f2(11)\r
52 \r
53 def blah(res):\r
54     print "look mama, no hands! res = %r" % (res.result,)\r
55 \r
56 # set the on_ready callback -- when r1 is becomes ready, the callback will\r
57 # be called automagically\r
58 r1.on_ready = blah \r
59 \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
62     print "!"\r
63     time.sleep(1)\r
64 \r
65 \r
66 \r