pull in additional changes from 2.0 branch.
[monitor.git] / monitor / Rpyc / Demo / demo-5.py
diff --git a/monitor/Rpyc/Demo/demo-5.py b/monitor/Rpyc/Demo/demo-5.py
new file mode 100644 (file)
index 0000000..4ea4491
--- /dev/null
@@ -0,0 +1,66 @@
+#\r
+# this demo will show you working with asynch proxies and callback\r
+# verison 2.3 removes the AsyncCallback factory, and instead provides a mechanism\r
+# where async results can provide a callback. it simplifies the design, so i\r
+# went for it.\r
+#\r
+import time\r
+from Rpyc.Factories import SocketConnection, Async\r
+\r
+c1 = SocketConnection("localhost")\r
+\r
+# f1 is an async proxy to the server's sleep function\r
+f1 = Async(c1.modules.time.sleep)\r
+\r
+# this would block the server for 9 seconds\r
+r1 = f1(9)\r
+# and this would block it for 11\r
+r2 = f1(11)\r
+\r
+# of course the client isnt affected (that's the whole point of Async)\r
+# but since the same server can't block simultaneously, the second request is\r
+# queued. this is a good example of queuing.\r
+\r
+# now we'll wait for both results to finish. this should print around 20 lines\r
+# (more or less, depending on the phase)\r
+while not r1.is_ready or not r2.is_ready:\r
+    print "!"\r
+    time.sleep(1)\r
+\r
+print "---"\r
+\r
+# now we'll dig in the h4xx0r shit -- running things simultaneously\r
+# for this, we'll need another connection, and another proxy:\r
+c2 = SocketConnection("localhost")\r
+f2 = Async(c2.modules.time.sleep)\r
+\r
+# now we'll do the same as the above, but this time, it will happen simulatenously\r
+# becuase f1 and f2 work on different connections\r
+r1 = f1(9)\r
+r2 = f2(11)\r
+\r
+# so this time, it will print around 11 lines\r
+while not r1.is_ready or not r2.is_ready:\r
+    print "!"\r
+    time.sleep(1)\r
+\r
+print "---"\r
+\r
+# very haxxor indeed. now, we'll see how to use the on_ready callback\r
+r1 = f1(9)\r
+r2 = f2(11)\r
+\r
+def blah(res):\r
+    print "look mama, no hands! res = %r" % (res.result,)\r
+\r
+# set the on_ready callback -- when r1 is becomes ready, the callback will\r
+# be called automagically\r
+r1.on_ready = blah \r
+\r
+# this should print 9 "!", then "look mama", then two more "!"\r
+while not r1.is_ready or not r2.is_ready:\r
+    print "!"\r
+    time.sleep(1)\r
+\r
+\r
+\r