File: README.rst

package info (click to toggle)
python-socketpool 0.5.3-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 184 kB
  • sloc: python: 629; makefile: 4
file content (131 lines) | stat: -rw-r--r-- 3,570 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
socketpool
----------

Socketpool - a simple Python socket pool.

Socket pool is a simple socket pool that suports multiple factories and
backends. It can easily be used by gevent, eventlet or any other library.

Usage
-----

socketpool offers 3 main classes, a `ConnectionPool` class able to
accept a factory and a backend, `Connector` an interface class
inherited by all connectors and a default TCP connector `TcpConnector` .


Example of a simple echo client using Gevent
--------------------------------------------

::

    import gevent
    from gevent.server import StreamServer

    from socketpool import ConnectionPool, TcpConnector

    # this handler will be run for each incoming connection
    # in a dedicated greenlet
    def echo(sock, address):
        print ('New connection from %s:%s' % address)

        while True:
            data = sock.recv(1024)
            if not data:
                break
            sock.send(data)
            print ("echoed %r" % data)



    if __name__ == '__main__':
        import time

        options = {'host': 'localhost', 'port': 6000}
        pool = ConnectionPool(factory=TcpConnector, backend="gevent")
        server = StreamServer(('localhost', 6000), echo)
        gevent.spawn(server.serve_forever)


        def runpool(data):
            print 'ok'
            with pool.connection(**options) as conn:
                print 'sending'
                sent = conn.send(data)
                print 'send %d bytes' % sent
                echo_data = conn.recv(1024)
                print "got %s" % data
                assert data == echo_data

        start = time.time()
        jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(20)]

        gevent.joinall(jobs)
        delay = time.time() - start


Example of a connector
----------------------

::

    class TcpConnector(Connector):

        def __init__(self, host, port, backend_mod, pool=None):
            self._s = backend_mod.Socket(socket.AF_INET, socket.SOCK_STREAM)
            self._s.connect((host, port))
            self.host = host
            self.port = port
            self._connected = True
            self._life = time.time()
            self._pool = pool
    
        def __del__(self):
            self.release()

        def matches(self, **match_options):
            target_host = match_options.get('host')
            target_port = match_options.get('port')
            return target_host == self.host and target_port == self.port

        def is_connected(self):
            return self._connected

        def handle_exception(self, exception):
            print 'got an exception'
            print str(exception)

        def get_lifetime(self):
            return self._life

        def invalidate(self):
            self._s.close()
            self._connected = False
            self._life = -1

        def release(self):
            if self._pool is not None:
                if self._connected:
                    self._pool.release_connection(self)
                else:
                    self._pool = None

        def send(self, data):
            return self._s.send(data)

        def recv(self, size=1024):
            return self._s.recv(size)


Authors
-------

- BenoƮt Chesneau (benoitc) <benoitc@e-engura.org>
- Tarek Ziade (tarek) <tarek@ziade.org>

License
-------

socketpool is available in the public domain (see UNLICENSE). socketpool
is also optionally available under the MIT License (see LICENSE), meant
especially for jurisdictions that do not recognize public domain works.