1from __future__ import print_function
2import sys
3import Pyro4
4
5
6if sys.version_info < (3, 0):
7    input = raw_input
8
9uri = input("enter async server object uri: ").strip()
10proxy = Pyro4.Proxy(uri)
11
12print("* normal call: (notice the delay)")
13print("result=", proxy.divide(100, 5))
14
15print("\n* async call:")
16proxy._pyroAsync()
17asyncresult = proxy.divide(100, 5)  # returns immediately
18print("result value available?", asyncresult.ready)  # prints False because the server is still 'busy'
19print("client can do other stuff here.")
20print("getting result value...(will block until available)")
21print("resultvalue=", asyncresult.value)  # blocks until the result is available
22
23print("\n* async call, with normal call inbetween:")
24normalproxy = Pyro4.Proxy(uri)
25asyncresult = proxy.divide(100, 5)  # returns immediately
26print("client does normal call: ", normalproxy.multiply(5, 20))
27print("client does normal call: ", normalproxy.multiply(5, 30))
28print("getting result value of async call...(will block until available)")
29print("resultvalue=", asyncresult.value)  # blocks until the result is available
30
31print("\n* async call with exception:")
32asyncresult = proxy.divide(100, 0)  # will trigger a zero division error, 100//0
33print("getting result value...")
34try:
35    value = asyncresult.value
36    print("Weird, this shouldn't succeed!?... resultvalue=", value)
37except ZeroDivisionError as x:
38    print("got exception (expected):", repr(x))
39
40print("\n* async call with timeout:")
41asyncresult = proxy.divide(100, 5)
42print("checking if ready within 2 seconds...")
43ready = asyncresult.wait(2)  # wait for ready within 2 seconds but the server takes 3
44print("status after waiting=", ready)  # should print False
45print("checking again if ready within 5 seconds...(should be ok now)")
46ready = asyncresult.wait(timeout=5)  # wait 5 seconds now (but server will be done within 1 more second)
47print("status after waiting=", ready)
48print("available=", asyncresult.ready)
49print("resultvalue=", asyncresult.value)
50
51print("\n* a few async calls at the same time:")
52results = [
53    proxy.divide(100, 7),
54    proxy.divide(100, 6),
55    proxy.divide(100, 5),
56    proxy.divide(100, 4),
57    proxy.divide(100, 3),
58]
59print("getting values...")
60for result in results:
61    print("result=", result.value)
62
63print("\ndone.")
64