2 # this is the grand finale: asynchronous proxies as super-events
\r
4 from Rpyc.Factories import SocketConnection, Async
\r
5 from Rpyc.Utils import *
\r
7 c = SocketConnection("localhost")
\r
10 # this is the remote int type
\r
12 rint = c.modules.__builtin__.int
\r
15 # and we'll wrap it in an asynchronous wrapper
\r
20 # now it still looks like a normal proxy... but operations on it return something called
\r
21 # an AsyncResult -- it's an object that represents the would-be result of the operation.
\r
22 # it has a .is_ready property, which indicates whether or not the result is ready, and
\r
23 # a .result property, which holds the result of the operations. when you access the .result
\r
24 # property, it will block until the result is returned
\r
27 b = rint("metallica")
\r
34 # and when an exception occurs, it looks like that
\r
42 # only when you access the result you get the exception, which may look weird, but hey,
\r
43 # it's an asynchronous world out there.
\r
47 # there's another methodology for async proxies -- on_ready callbacks. instead of
\r
48 # getting the async result, you can register a callback to collect it, when it arrives.
\r
51 print "the result is",
\r
55 print "an exception"
\r
57 rint = Async(c.modules.__builtin__.int)
\r
62 # this will cause an exception
\r
63 ar = rint("a perfect circle")
\r
66 # or when you dont need to keep the async result
\r
67 rint("456").on_ready = f
\r
69 # and it's not limited to calling it. anything you do to the async proxy is asynchronous.
\r
70 # for example, you can also get attributes asynchronously:
\r
74 # now we'll do some other request, which will cause the results to arrive, and the callback
\r
79 ############################################################################################
\r
81 # this is where we get hardcore: threads and event callbacks
\r
89 # we'll start a thread on the server which on threadfunc (which is defined in the testmodule).
\r
90 # this function will call the callback we give it every second, but will ignore the result.
\r
91 # this practically means it's like an event -- trigger and forget. on the client side, the
\r
92 # callback will increment `xxx` every time it's called
\r
94 c.modules.thread.start_new_thread(c.modules.Rpyc.Demo.testmodule.threadfunc, (blah,))
\r
97 # we'll wait a little
\r
103 # and do some operation, which, along with it, will pull all incoming requests
\r
105 print c.modules.sys
\r
109 # and we can start a thread of our own to pull the requests in the background
\r
112 worker_running = True
\r
115 while worker_running:
\r
118 thread.start_new_thread(worker, (c,))
\r
121 worker_running = False
\r