Welcome, guest | Sign In | My Account | Store | Cart

How to transfer large files with Twisted Perspective Broker using twisted.spread.util.Pager and friends. Code copied and pasted from here: http://twistedmatrix.com/pipermail/twisted-python/2003-May/004019.html

Just wanted to make it easier to find. Originally by: Glyph Lefkowitz

Python, 91 lines
 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
from twisted.spread.util import StringPager
from twisted.spread.flavors import Referenceable
from twisted.internet.defer import Deferred

### Server Side

class ResultsPager(StringPager):
    def __init__(self, collector, string, firstPage):
        self._atFirstPage = True
        self._firstPage = firstPage
        self._deferred = Deferred()
        StringPager.__init__(self, collector, string, callback=self.done)

    def nextPage(self):
        if self._atFirstPage:
            self._atFirstPage = False
            return self._firstPage
        else:
            return StringPager.nextPage(self)

    def done(self):
        self._deferred.callback(self.collector)

    def wait(self):
        return self._deferred

class DatabaseThingy(Referenceable):
    def remote_getResult(self, collector, phrase):
        st = phrase * 1024 * 1024
        lp = 1, 2, phrase * 2
        pager = ResultsPager(collector, st, lp)
        return pager.wait()

### Client Side

class SimplePageCollector(Referenceable):
    def __init__(self):
        self.pages = []
    def remote_gotPage(self, page):
        self.pages.append(page)
    def remote_endedPaging(self):
        print 'paging ended'

class ResultGetter:
    def __init__(self, dt):
        self.dataThingy = dt

    def getRemoteResult(self):
        return self.dataThingy.callRemote(
            "getResult",
            SimplePageCollector(),
            "hello world ").addCallback(self._massage
                                        ).addCallbacks(self.ok, self.nok)
    
    def _massage(self, collector):
        pages = collector.pages
        bigstr = ''.join(pages[1:])
        return tuple(pages[0])+ (bigstr,)

        return collector.args()

    def ok(self, (int1, int2, shortString, hugeString)):
        print 'data ok'
        print int1, int2, repr(shortString), len(hugeString)

    def nok(self, f):
        print 'data not ok'
        return f

if __name__ == '__main__':
    import sys
    from twisted.internet import reactor
    from twisted.python import log
    log.startLogging(sys.stdout)
    PORTNO = 8123
    if sys.argv[1] == 'server':
        from twisted.spread.flavors import Root
        from twisted.spread.pb import BrokerFactory
        class SimpleRoot(Root):
            def rootObject(self, broker):
                return DatabaseThingy()
        reactor.listenTCP(PORTNO, BrokerFactory(SimpleRoot()))
    elif sys.argv[1] == 'client':
        from twisted.spread.pb import getObjectAt
        def getIt(x):
            r = ResultGetter(x)
            return r.getRemoteResult()
        getObjectAt("localhost", PORTNO).addCallback(getIt)
    else:
        raise sys.exit("usage: %s (server|client)" % sys.argv[0])
    reactor.run()

1 comment

alex lavoro.propio 14 years, 11 months ago  # | flag

The code doens't work for Twisted 8.2.0. I believe it should be:

if sys.argv[1] == 'server':
    from twisted.spread.flavors import Root
    from twisted.spread.pb import PBServerFactory 
    class SimpleRoot(Root):
        def rootObject(self, broker):
            return DatabaseThingy()
    reactor.listenTCP(PORTNO, PBServerFactory(SimpleRoot()))
elif sys.argv[1] == 'client':
    from twisted.spread import pb
    def getIt(x):
        r = ResultGetter(x)
        return r.getRemoteResult()
    cf = pb.PBClientFactory()
    reactor.connectTCP("localhost", PORTNO, cf)
    cf.getRootObject().addCallback(getIt)