Skip to main content

A simple connection pool for gevent

Project description

https://badge.fury.io/py/gsocketpool.png https://travis-ci.org/studio-ousia/gsocketpool.png?branch=master

A simple connection pool for gevent.

Basic Usage

The following is an example to create a connection pool that communicates an echo server running on localhost 2000.

>>> from gsocketpool import Pool
>>> from gsocketpool import TcpConnection
>>>
>>> options = dict(host='localhost', port=2000)
>>> pool = Pool(TcpConnection, options)
>>>
>>> with pool.connection() as conn:
...     conn.send('hello')
...     print conn.recv()
hello

Implementing Protocol

Arbitrary protocols can be easily implemented by extending Connection class. You have to override at least three functions such as open(), close() and is_connected().

TcpConnection used in the above example is also implemented as a subclass of Connection.

class TcpConnection(Connection):

    def __init__(self, host, port, lifetime=600, timeout=None):
        self._sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        self._host = host
        self._port = port
        self._lifetime = lifetime
        self._timeout = timeout
        self._connected = False
        self._created = None

    def get(self):
        return self._sock

    def open(self):
        self._sock.connect((self._host, self._port))
        if self._timeout:
            self._sock.settimeout(self._timeout)

        self._connected = True
        self._created = time.time()

    def close(self):
        if self._connected:
            self._sock.close()
            self._connected = False

    def is_connected(self):
        return self._connected

    def is_expired(self):
        if time.time() - self._created > self._lifetime:
            return True
        else:
            return False

    def send(self, data):
        assert self._connected

        self._sock.send(data)

    def recv(self, size=1024):
        assert self._connected

        return self._sock.recv(size)

Documentation

Documentation is available at http://gsocketpool.readthedocs.org/.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

gsocketpool-0.1.6.tar.gz (5.5 kB view details)

Uploaded Source

File details

Details for the file gsocketpool-0.1.6.tar.gz.

File metadata

  • Download URL: gsocketpool-0.1.6.tar.gz
  • Upload date:
  • Size: 5.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for gsocketpool-0.1.6.tar.gz
Algorithm Hash digest
SHA256 f2e2749aceadce6b27ca52e2b0a64af99797746a8681e1a2963f72007c14cb14
MD5 49f5f292ef1b60944ae92ca426a5e550
BLAKE2b-256 af3da9a5636e855908375a3a06e2eada48ce1bf2bd6eba07651dd019772f648d

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page