-
Notifications
You must be signed in to change notification settings - Fork 36
/
client.py
64 lines (43 loc) · 1.74 KB
/
client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import time
import threading
import Pyro5.api
import Pyro5.errors
proxy = Pyro5.api.locate_ns() # grab a proxy for the name server
print("Main thread:", threading.current_thread())
proxy.ping() # call it, the proxy is now connected and bound to the main thread
# trying to use the proxy in a different thread is not possible,
# and Pyro will raise an exception to tell you that:
def other_thread_call():
try:
proxy.ping()
print("You should not see this!! the call succeeded in thread", threading.current_thread())
except Pyro5.errors.PyroError as x:
print("Expected exception in thread", threading.current_thread())
print("Exception was: ", x)
print()
threading.Thread(target=other_thread_call).start()
time.sleep(1)
# SOLUTION 1: create a new proxy in the other thread.
def new_proxy_thread_call(uri):
proxy = Pyro5.api.Proxy(uri)
proxy.ping()
print("Solution 1. The call succeeded in thread", threading.current_thread())
print()
threading.Thread(target=new_proxy_thread_call, args=(proxy._pyroUri,)).start()
time.sleep(1)
# SOLUTION 2: transfer ownership of our proxy to the other thread.
def new_owner_thread_call(proxy):
proxy._pyroClaimOwnership()
proxy.ping()
print("Solution 2. The call succeeded in thread", threading.current_thread())
print()
threading.Thread(target=new_owner_thread_call, args=(proxy,)).start()
time.sleep(1)
# however, we are no longer the owner of the proxy now, so any new calls will fail for us
print()
try:
proxy.ping()
print("You should not see this!! the call succeeded in thread", threading.current_thread())
except Pyro5.errors.PyroError as x:
print("Expected exception in thread", threading.current_thread())
print("Exception was: ", x)