skip to navigation
skip to content

gsocketpool 0.1.3

A simple connection pool for gevent

Latest Version: 0.1.5

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()

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._connected = True
        self._created = time.time()

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

    def is_connected(self):
        return self._connected

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

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


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

        return self._sock.recv(size)


Documentation is available at

File Type Py Version Uploaded on Size
gsocketpool-0.1.3.tar.gz (md5) Source 2014-04-06 5KB
  • Downloads (All Versions):
  • 52 downloads in the last day
  • 202 downloads in the last week
  • 931 downloads in the last month
  • Author: Studio Ousia
  • Home Page:
  • License:
    Copyright 2013 Studio Ousia
       Licensed under the Apache License, Version 2.0 (the "License");
       you may not use this file except in compliance with the License.
       You may obtain a copy of the License at
       Unless required by applicable law or agreed to in writing, software
       distributed under the License is distributed on an "AS IS" BASIS,
       WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
       See the License for the specific language governing permissions and
       limitations under the License.
  • Package Index Owner: ikuyamada, ousia
  • DOAP record: gsocketpool-0.1.3.xml