def test_stop_reaper_thread(self): """Verify that calling stop_reaper will terminate the reaper thread. """ pool = ConnectionPool(MessyConnector, backend='thread') assert pool._reaper.running pool.stop_reaper() for i in xrange(1000): if not pool._reaper.running: return time.sleep(0.01) assert False, 'Reaper thread not terminated in time.'
def test_del(self): """Verify that garbage collection of the pool will release the reaper thread. """ pool = ConnectionPool(MessyConnector, backend='thread') reaper = pool._reaper assert reaper.running # Remove reference. pool = None for i in xrange(1000): if not reaper.running: return time.sleep(0.01) assert False, 'Reaper thread not terminated in time.'
data = sock.recv(1024) if not data: break sock.send(data) print ("echoed %r" % data) def stop(self): self.running = False if __name__ == '__main__': import time options = {'host': 'localhost', 'port': 6000} pool = ConnectionPool(factory=TcpConnector, options=options, backend="eventlet") server = EchoServer('localhost', 6000) server.start() epool = eventlet.GreenPool() 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()
data = sock.recv(1024) if not data: break sock.send(data) print("echoed %r" % data) def stop(self): self.running = False if __name__ == '__main__': import time options = {'host': 'localhost', 'port': 6000} pool = ConnectionPool(factory=TcpConnector, options=options, backend="eventlet") server = EchoServer('localhost', 6000) server.start() epool = eventlet.GreenPool() 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
# Port 0 means to select an arbitrary unused port HOST, PORT = "localhost", 0 server = EchoServer((HOST, PORT), EchoHandler) ip, port = server.server_address # Start a thread with the server -- that thread will then start one # more thread for each request server_thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval":0.5}) # Exit the server thread when the main thread terminates server_thread.daemon = True server_thread.start() options = {'host': ip, 'port': port} pool = ConnectionPool(factory=TcpConnector, options=options) q = Queue() def runpool(): while True: try: data = q.get(False) except Empty: break try: with pool.connection() as conn: print("conn: pool size: %s" % pool.size()) sent = conn.send(data) echo = conn.recv(1024) print("got %s" % data) assert data == echo
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", max_size=5) server = StreamServer(('localhost', 6000), echo) gevent.spawn(server.serve_forever) def runpool(data): with pool.connection(**options) as conn: print("conn: pool size: %s" % pool.size) sent = conn.send(data) echo_data = conn.recv(1024) assert data == echo_data start = time.time() jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(50)] gevent.joinall(jobs)
def make_and_check_backend(self, expected_name, **kw): pool = ConnectionPool(TcpConnector, **kw) assert pool.backend == expected_name return pool
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): with pool.connection(**options) as conn: print("conn: pool size: %s" % pool.size) sent = conn.send(data) echo_data = conn.recv(1024) assert data == echo_data start = time.time() jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(50)] gevent.joinall(jobs)
# # This file is part of socketpool. # See the NOTICE for more information. import gevent from socketpool.pool import ConnectionPool from socketpool.conn import DeviceConnector if __name__ == '__main__': import time conInfo = "ssh#192.168.1.95#admin#Sdntiger" options = {'devType': 'huawei', 'connInfo': conInfo} pool = ConnectionPool(factory=DeviceConnector, backend="gevent", max_size=2) def runpool(data): with pool.connection(**options) as conn: print("conn: pool size: %s" % pool.size) print conn start = time.time() jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(6)] gevent.joinall(jobs) delay = time.time() - start print("final pool size: %s" % pool.size)
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): with pool.connection(**options) as conn: print ("conn: pool size: %s" % pool.size()) sent = conn.send(data) echo_data = conn.recv(1024) assert data == echo_data start = time.time() jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(50)]
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): with pool.connection(**options) as conn: print("conn: pool size: %s" % pool.size) sent = conn.send(data) echo_data = conn.recv(1024) assert data == echo_data start = time.time() jobs = [gevent.spawn(runpool, "blahblah") for _ in xrange(50)] gevent.joinall(jobs)
# Port 0 means to select an arbitrary unused port HOST, PORT = "localhost", 0 server = EchoServer((HOST, PORT), EchoHandler) ip, port = server.server_address # Start a thread with the server -- that thread will then start one # more thread for each request server_thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval":0.5}) # Exit the server thread when the main thread terminates server_thread.daemon = True server_thread.start() options = {'host': ip, 'port': port} pool = ConnectionPool(factory=TcpConnector, options=options) q = Queue() def runpool(): while True: try: data = q.get(False) except Empty: break try: with pool.connection() as conn: print("conn: pool size: %s" % pool.size) sent = conn.send(data) echo = conn.recv(1024) print("got %s" % data) assert data == echo
# Port 0 means to select an arbitrary unused port HOST, PORT = "localhost", 0 server = EchoServer((HOST, PORT), EchoHandler) ip, port = server.server_address # Start a thread with the server -- that thread will then start one # more thread for each request server_thread = threading.Thread(target=server.serve_forever, kwargs={"poll_interval": 0.5}) # Exit the server thread when the main thread terminates server_thread.daemon = True server_thread.start() options = {'host': ip, 'port': port} pool = ConnectionPool(factory=TcpConnector, options=options) q = Queue() def runpool(): while True: try: data = q.get(False) except Empty: break try: with pool.connection() as conn: print("conn: pool size: %s" % pool.size) sent = conn.send(data) echo = conn.recv(1024) print("got %s" % data) assert data == echo
def test_size_on_isconnected_failure(self): pool = ConnectionPool(MessyConnector) assert pool.size == 0 pytest.raises(MaxTriesError, pool.get)