Skip to main content

Python socket pool

Project description

socketpool

Socketpool - a simple Python socket pool.

Socket pool is a simple socket pool that sumport multiple factory and backend. It can easily be used by gevent, eventlet or any other library.

Usage

socketpool offers 3 main classes, a ConnectionPool class abble to accept a factory and a backend, Connector and interface class inherited by all connector 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, options=options,
            backend="gevent")
    server = StreamServer(('localhost', 6000), echo)
    gevent.spawn(server.serve_forever)


    def runpool(data):
        print 'ok'
        with pool.connection() 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):
        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()

    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 send(self, data):
        return self._s.send(data)

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

Authors

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.

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

socketpool-0.1.tar.gz (7.7 kB view details)

Uploaded Source

File details

Details for the file socketpool-0.1.tar.gz.

File metadata

  • Download URL: socketpool-0.1.tar.gz
  • Upload date:
  • Size: 7.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for socketpool-0.1.tar.gz
Algorithm Hash digest
SHA256 3801643c59d1d09a88c1a9393203cfcea5d58d380e14c5bb548634090529a976
MD5 9a3610b4847ae515ff444b8157a8a256
BLAKE2b-256 e80feb003af62afe956c018133bad68dde9034b3118d148e5286bc3e8c92aac4

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