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 finally: q.task_done() for i in range(20): q.put(s2b("Hello World %s" % i), False) for i in range(4): th = threading.Thread(target=runpool) th.daemnon = True th.start() q.join() print ("final pool size: %s" % pool.size()) pool.release_all() server.shutdown()
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) delay = time.time() - start print ("final pool size: %s" % pool.size()) with pool.connection(**options) as conn: print ("conn: pool size: %s" % pool.size()) sent = conn.send("hello") echo_data = conn.recv(1024) assert "hello" == echo_data print ("final pool size: %s" % pool.size())