File: client.py

package info (click to toggle)
pyro4 4.82-2
  • links: PTS
  • area: main
  • in suites: bookworm
  • size: 2,528 kB
  • sloc: python: 17,736; makefile: 169; sh: 113; javascript: 62
file content (73 lines) | stat: -rw-r--r-- 2,244 bytes parent folder | download | duplicates (3)
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
65
66
67
68
69
70
71
72
73
from __future__ import print_function
import threading
import time
import sys
import Pyro4


if sys.version_info < (3, 0):
    input = raw_input


def get_user_token():
    return "user123"


class CustomAnnotationProxy(Pyro4.Proxy):
    # override the method that adds annotations and add our own custom user token annotation
    def _pyroAnnotations(self):
        return {"USER": get_user_token().encode("utf-8")}


class DbAccessor(threading.Thread):
    def __init__(self, uri):
        super(DbAccessor, self).__init__()
        self.proxy = CustomAnnotationProxy(uri)
        self.daemon = True

    def run(self):
        for i in range(3):
            try:
                self.proxy.store("number", 100+i)
                num = self.proxy.retrieve("number")
                print("[%s] num=%s" % (self.name, num))
            except Exception:
                import traceback
                traceback.print_exc()


print("\n***** Sequential access using multiple proxies on the Session-Bound Database... (no issues)")
with CustomAnnotationProxy("PYRONAME:example.usersession.sessiondb") as p1,\
        CustomAnnotationProxy("PYRONAME:example.usersession.sessiondb") as p2:
    p1.store("number", 42)
    p1.retrieve("number")
    p2.store("number", 43)
    p2.retrieve("number")

print("\n***** Sequential access using multiple proxies on the Singleton Database... (no issues)")
with CustomAnnotationProxy("PYRONAME:example.usersession.singletondb") as p1,\
        CustomAnnotationProxy("PYRONAME:example.usersession.singletondb") as p2:
    p1.store("number", 42)
    p1.retrieve("number")
    p2.store("number", 43)
    p2.retrieve("number")

print("\n***** Multiple concurrent proxies on the Session-Bound Database... (no issues)")
input("enter to start: ")
t1 = DbAccessor("PYRONAME:example.usersession.sessiondb")
t2 = DbAccessor("PYRONAME:example.usersession.sessiondb")
t1.start()
t2.start()
time.sleep(1)
t1.join()
t2.join()

print("\n***** Multiple concurrent proxies on the Singleton Database... (threading problem)")
input("enter to start: ")
t1 = DbAccessor("PYRONAME:example.usersession.singletondb")
t2 = DbAccessor("PYRONAME:example.usersession.singletondb")
t1.start()
t2.start()
time.sleep(1)
t1.join()
t2.join()