File: timeout2.py

package info (click to toggle)
pyro 1%3A3.14-5
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,048 kB
  • ctags: 1,988
  • sloc: python: 11,194; xml: 128; sh: 52; makefile: 28
file content (73 lines) | stat: -rw-r--r-- 1,785 bytes parent folder | download | duplicates (6)
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
import time
import Pyro.core
import Pyro.naming
from Pyro.errors import NamingError
from threading import Thread

class MySimpleServer(Thread):
    def __init__(self, daemon):
        super(MySimpleServer, self).__init__()
        self.__oDaemon = daemon
        self.__tIsRuning = False
        self.__tIsStopped = False
        self.setDaemon(True)

    def run(self):
        self.__tIsRunning = True
        while self.__tIsRunning:
            time.sleep(.1)
            self.__oDaemon.handleRequests(3)
        self.__oDaemon.shutdown()
        self.__tIsStopped = True
        print "server stopped."

    def stop(self):
        self.__tIsRunning = False

    def isStopped(self):
        return self.__tIsStopped


class MyObject(Pyro.core.ObjBase):
    def test(self):
        print 'test called'

Pyro.core.initServer()

oNs = Pyro.naming.NameServerLocator().getNS()
oDaemon = Pyro.core.Daemon()
oDaemon.useNameServer(oNs)


strTag = 'timeout_example'
try:
    oNs.unregister(strTag)
except NamingError:
    pass

oObj = MyObject()
strUri = oDaemon.connect(oObj, strTag)

oServer = MySimpleServer(oDaemon)
oServer.start()

oProxy = Pyro.core.getProxyForURI(strUri)
# not setting timeout this time:  oProxy._setTimeout(4)

print 'calling remote method on live proxy.'
oProxy.test()
print 'success.'

print "stopping server..."
oServer.stop()
while not oServer.isStopped():
    time.sleep(.1)

print
print 'calling remote method on stale proxy.'
print 'as we did not specify a timeout, this should block indefinitely.'
print '(press ctrl-c or ctrl-break to abort the program)'
oProxy = Pyro.core.getProxyForURI(strUri)
# not setting timeout this time:   oProxy._setTimeout(4)
oProxy.test()
print 'This should not be shown!!! The method call should have blocked indefinitely!'