A simple connection pool for gevent
Project description
A simple connection pool for gevent.
Basic Usage
The following is an example to create a connection pool that communicats an echo server running on localhost 2000.
>>> from gsocketpool.pool import Pool
>>> from gsocketpool.connection 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
@property
def socket(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.0.1.tar.gz
(5.3 kB
view details)
Built Distribution
gsocketpool-0.0.1-py2.7.egg
(13.7 kB
view details)
File details
Details for the file gsocketpool-0.0.1.tar.gz
.
File metadata
- Download URL: gsocketpool-0.0.1.tar.gz
- Upload date:
- Size: 5.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 5685197d69fb77fb3f5d95550d9e3f575180c136fff8a848cb8a5a82f0e49225 |
|
MD5 | 5533b64d42f1db94fd684c9effe2eba9 |
|
BLAKE2b-256 | 72e6a50cf60e86fa47a8143ca4dc1a3a7b010eb7540c523b9749a545f2563ad1 |
File details
Details for the file gsocketpool-0.0.1-py2.7.egg
.
File metadata
- Download URL: gsocketpool-0.0.1-py2.7.egg
- Upload date:
- Size: 13.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | bcdfbaf73684906706426c29da5f91f53248aa3e8b274ba907c45ea578642de9 |
|
MD5 | dfd661ccc4ce968f7f10137b3b71c8be |
|
BLAKE2b-256 | 2eac19bea49f5e087cb664099fc7e5f3c61df4dcb9de84b0b89631b14d48fe79 |